O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Log
46 %macro obsnvars(ds);
47 %global dset nvars nobs;
48 %let dset=&ds;
49 %let dsid = %sysfunc(open(&dset));
50
51 %if &dsid %then %do;
52 %let nobs =%sysfunc(attrn(&dsid,nlobs));
53 %let nvars=%sysfunc(attrn(&dsid,nvars));
54 %let rc = %sysfunc(close(&dsid));
55 %end;
56
57 %else %put open for data set &dset failed - %sysfunc(sysmsg());
58 %mend obsnvars;
59
60 %obsnvars(test)
61
62 %put &dset has &nvars variable(s) and &nobs observation(s).;
test has 4 variable(s) and 2 observation(s).
Example 9: Use Macro Logic to Determine If an
External File Is Empty
Details
This macro verifies that an external file exists. If it does not exist, then a message is
written to the log. If the file does exist, then the file is opened and SAS attempts to read
the data within the file. Functions, such as FOPEN, FREAD and FGET, are used to
retrieve the data. If there is no data to be read, a message is written to the log that the file
is empty.
Program
%macro test(outf);
%let filrf=myfile;
%if %sysfunc(fileexist(&outf)) %then %do;
%let rc=%sysfunc(filename(filrf,&outf));
%let fid=%sysfunc(fopen(&filrf));
%if &fid > 0 %then %do;
%let rc=%sysfunc(fread(&fid));
%let rc=%sysfunc(fget(&fid,mystring));
%if &rc = 0 %then %put &mystring;
%else %put file is empty;
%let rc=%sysfunc(fclose(&fid));
%end;
%let rc=%sysfunc(filename(filrf));
%end;
%else %put file does not exist;
%mend test;
464 Appendix 5 SAS Macro Examples
%test(c:\test.txt)
Program Description
Begin the macro definition with one parameter.
%macro test(outf);
Use the %LET statement to create a macro variable named &FILRF that will contain the
fileref name that will be used later.
%let filrf=myfile;
Use the %IF statement with the FILEEXIST function to verify that the file that is passed
to the macro does exist.
%if %sysfunc(fileexist(&outf)) %then %do;
Use the FILENAME function to associate a fileref with the file that is passed into the
macro named &OUTF. Use the FOPEN function to open that file and assign a file
identifier value to the macro variable &FID.
%let rc=%sysfunc(filename(filrf,&outf));
%let fid=%sysfunc(fopen(&filrf));
Use the %IF statement to make sure the file was successfully opened. FOPEN returns a
0 if the file could not be open and a nonzero if the file was successfully open.
%if &fid > 0 %then %do;
Use the FREAD function to read a record from the file. Use the FGET function to copy
the data into a macro variable named &MYSTRING.
%let rc=%sysfunc(fread(&fid));
%let rc=%sysfunc(fget(&fid,mystring));
Use the %IF statement to make sure the FGET function was successful. The FGET
function returns a 0 if the operation was successful. If the FGET function was
successful, then use a %PUT statement to write the contents of the variable named
&MYSTRING to the log.
%if &rc = 0 %then %put &mystring;
If the %IF condition is false, then use a %PUT statement to write a message to the log
stating that the file is empty.
%else %put file is empty;
Use the FCLOSE function to close the file.
%let rc=%sysfunc(fclose(&fid));
End the inner %IF %THEN %DO block.
%end;
Use the FILENAME function to disassociate the fileref from the file.
%let rc=%sysfunc(filename(filrf));
End the outer %IF %THEN %DO block.
%end;
If the %IF statement that uses the FILEEXIST function is false, then use the %PUT
statement to write that the file does not exist to the log.
%else %put file does not exist;
Example 9: Use Macro Logic to Determine If an External File Is Empty 465

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required