BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

%maco check;

%global exists findparm; 

 

%let exists=%sysfunc(exist(mypath.dataset));

%if &exist %then %do; 

%let findparm = -type f - newer &thatpath/dataset.sas7bdat;

%end;

%else %do;

%let findparm= -type f; 

%end;

mend;

 

Can someone explain line by line please?

Respectfully ,

blue blue

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Let's do it line by line.

 

%maco check;

That should be

 

 

%macro check;

And it begins the definition of a macro named CHECK.

 

 

%global exists findparm; 

That will force two macro variables, EXISTS and FINDPARM, into the GLOBAL macro scope.  If there such macro variables do not exist there values will be empty strings.  If they exist and are already GLOBAL then nothing changes.  If they exist but are LOCAL to some other macro that has called this %CHECK macro then you will get an ERROR message.

 

 

%let exists=%sysfunc(exist(mypath.dataset));

That assigns a value to the EXISTS macro.  It will be the result of calling the SAS function EXIST() ,via the macro function %SYSFUNC(), to test if the dataset DATASET exists in the library pointed to by the MYPATH libref.  The value will be either 1 (TRUE) or 0 (FALSE).

 

 

%if &exist %then %do; 
  %let findparm = -type f - newer &thatpath/dataset.sas7bdat;
%end;

That will conditionally set the value of the FINDPARM macro variable.  If the value in EXIST is TRUE then FINDPARM is going to be set to the string that starts with a hyphen.   Looks like they are perhaps command line options that will be used latter with some Unix command.

 

 

%else %do;
  %let findparm= -type f; 
%end;

But when the dataset did not exist it just set FINDPARM to that shorter string.

mend;

That should be:

%mend;

Which marks the end of the definition of the macro.

 

Once you have defined the macro you can then call it using 

%check

And it will update (and possibly create) the two macro variables based on whether or not the dataset exists.

 

Read about the scoping of macro variables https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p1b76sxg9dbcyrn1l5age5j5nvgw.htm

 

A macro variable that is in the LOCAL scope of the macro disappears when the macro finishes. The reason to use %GLOBAL is to ensure that the values assigned while teh macro runs are available after the macro has finished running.

 

Note that another way to make sure the macro variable's value exists after the macro finishes running is to define the macro variable before calling the macro.

 

To avoid the error that would occur if the macro variables already existed as LOCAL to some other macro that is calling %CHECK you should only conditionally run the %GLOBAL statement.

%if not %symexist(exists) %then %global exists;
%if not %symexist(findparm) %then %global findparm; 

 

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Macro variables named &EXISTS and &FINDPARM can be accessed outside the macro in which they were created. If there was no %GLOBAL statement, then these two macro variables could NOT be accessed outside the macro in which they were created.

 

%let exists= determines if the data set exists, and assigns a 1 if so and a zero otherwise

%if &exists if this is equal to 1, then certain commands are executed; otherwise other commands are executed. (Typo was fixed by me, the original code uses &exist which doesn't have a value)

%let findparm= assigns a text string to the macro variable 

--
Paige Miller
GN0001
Barite | Level 11
Thank you so very much for this explanation.
Respectfully
Bittenapple
Blue Blue
Tom
Super User Tom
Super User

Let's do it line by line.

 

%maco check;

That should be

 

 

%macro check;

And it begins the definition of a macro named CHECK.

 

 

%global exists findparm; 

That will force two macro variables, EXISTS and FINDPARM, into the GLOBAL macro scope.  If there such macro variables do not exist there values will be empty strings.  If they exist and are already GLOBAL then nothing changes.  If they exist but are LOCAL to some other macro that has called this %CHECK macro then you will get an ERROR message.

 

 

%let exists=%sysfunc(exist(mypath.dataset));

That assigns a value to the EXISTS macro.  It will be the result of calling the SAS function EXIST() ,via the macro function %SYSFUNC(), to test if the dataset DATASET exists in the library pointed to by the MYPATH libref.  The value will be either 1 (TRUE) or 0 (FALSE).

 

 

%if &exist %then %do; 
  %let findparm = -type f - newer &thatpath/dataset.sas7bdat;
%end;

That will conditionally set the value of the FINDPARM macro variable.  If the value in EXIST is TRUE then FINDPARM is going to be set to the string that starts with a hyphen.   Looks like they are perhaps command line options that will be used latter with some Unix command.

 

 

%else %do;
  %let findparm= -type f; 
%end;

But when the dataset did not exist it just set FINDPARM to that shorter string.

mend;

That should be:

%mend;

Which marks the end of the definition of the macro.

 

Once you have defined the macro you can then call it using 

%check

And it will update (and possibly create) the two macro variables based on whether or not the dataset exists.

 

Read about the scoping of macro variables https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p1b76sxg9dbcyrn1l5age5j5nvgw.htm

 

A macro variable that is in the LOCAL scope of the macro disappears when the macro finishes. The reason to use %GLOBAL is to ensure that the values assigned while teh macro runs are available after the macro has finished running.

 

Note that another way to make sure the macro variable's value exists after the macro finishes running is to define the macro variable before calling the macro.

 

To avoid the error that would occur if the macro variables already existed as LOCAL to some other macro that is calling %CHECK you should only conditionally run the %GLOBAL statement.

%if not %symexist(exists) %then %global exists;
%if not %symexist(findparm) %then %global findparm; 

 

 

GN0001
Barite | Level 11
Thanks from both for great explanation.
The only thing I didn’t understand is what -type f. -newer. I know the value of this string is assigned in findparm.
But what happens If we simply say - type without f and without -newer?
Respectfully
Bittenapple
Blue Blue
Tom
Super User Tom
Super User

@GN0001 wrote:
Thanks from both for great explanation.
The only thing I didn’t understand is what -type f. -newer. I know the value of this string is assigned in findparm.
But what happens If we simply say - type without f and without -newer?
Respectfully
Bittenapple

We don't know since the macro variable is not used for anything in the code you shared.

 

Like I said I expect it will be used to generate a Unix command.  From the name and the options mentioned I would guess the find command.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 372 views
  • 4 likes
  • 4 in conversation