Begin the DATA step.
data example;
input text $40.;
When using nested functions, start inside and work your way out. The inner most
function is QUOTE. It is used to add double quotation marks to the character value
contained within the variable TEXT. The RESOLVE function is used to resolve the
value of a text expression during the DATA step execution. In this case, the RESOLVE
function returns the value of the DATA step variable TEXT. The DEQUOTE function
removes matching quotation marks from a character string that begins with a quotation
mark. Then the DEQUOTE function deletes all characters to the right of the closing
quotation mark. The final value is assigned to the variable named TEXTRESOLVED.
textresolved=dequote(resolve(quote(text)));
Use the DATALINES statement to create the data.
datalines;
John's &dog
My &dog is a female
That's Amy's &dog puppy
;
Use PROC PRINT to display the results.
proc print;
run;
Output
Obs text textresolved
1 John's &dog John's Golden Retriever
2 My &dog is a female My Golden Retriever is a female
3 That's Amy's &dog puppy That's Amy's Golden Retriever puppy
Example 13: Print Information to the Output
Window If a Data Set Is Empty
Details
This macro uses the PRINT procedure to print a data set if it contains observations. If
there are no observations, then a DATA _NULL_ step is used to write a message to the
output window.
Program
data one;
x=1;
run;
data two;
468 Appendix 5 SAS Macro Examples
stop;
run;
%macro drive(dsn);
%let dsid=%sysfunc(open(&dsn));
%if &dsid ne 0 %then %do;
%let cnt=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));
%if &cnt ne 0 %then %do;
proc print data=&dsn;
title "This is data from data set &dsn";
run;
%end;
%else %do;
data _null_;
title;
file print;
put _page_;
put "Data set &dsn is empty.";
run;
%end;
%end;
%else %put &dsn cannot be open.;
%mend drive;
%drive(one)
%drive(two)
Program Description
Create a SAS data set containing an observation.
data one;
x=1;
run;
Create an empty data set.
data two;
stop;
run;
Begin the macro definition with one parameter.
%macro drive(dsn);
Use the OPEN function to open the data set that is passed to the macro. Use the %IF
condition to make sure the data set opened successfully.
%let dsid=%sysfunc(open(&dsn));
%if &dsid ne 0 %then %do;
Use the ATTRN function along with the NLOBS argument to determine the number of
observations in the data set that was opened in the OPEN function. Place this value into
a macro variable named &CNT.
%let cnt=%sysfunc(attrn(&dsid,nlobs));
Use the CLOSE function to close the data set.
Example 13: Print Information to the Output Window If a Data Set Is Empty 469
%let rc=%sysfunc(close(&dsid));
Use the %IF statement to check the value in &CNT. If &CNT is not equal to 0, then the
data set contains observations and PROC PRINT is executed.
%if &cnt ne 0 %then %do;
proc print data=&dsn;
title "This is data from data set &dsn";
run;
%end;
If the %IF statement is false, then the data set is empty and a DATA step is executed.
The FILE PRINT statement is used to direct the output that is produced by the PUT
statements to the same file as the output.
%else %do;
data _null_;
title;
file print;
put _page_;
put "Data set &dsn is empty.";
run;
%end;
%end;
If data set could not be opened, %ELSE is executed and uses %PUT to write a note to
the log.
%else %put &dsn cannot be open.;
End the macro.
%mend drive;
Invoke the macro by passing in the data set name.
%drive(one)
Here are the results for %drive(one).
Invoke the macro by passing in the data set name.
%drive(two)
Here are the results for %drive(two).
Output
Output: %drive(one)
Obs x
1 1
Output: %drive(two)
Data set two is empty.
470 Appendix 5 SAS Macro Examples

Get SAS 9.4 Macro Language, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.