BookmarkSubscribeRSS Feed
mancel3
Calcite | Level 5

Hi all, 

 

I am writing a program with a series of lines at the top to manually enter various macros. Based on what macros are entered, I want to be able to run different lines of code and am not sure if this is possible in sas. high level overiew of what I want:

 

%let var1 = 123;

%let var2 = 'string';

/* %let var3 = 567; */

/* %let var5 = 999;  */

 

if var3 is not null then 

          **proc sql code here**

 

else if var5 is not null then 

          **different proc sql code here** 

 

else if var1 is not null then 

          **different proc sql code again**

 end;

 

Is this possible in any way in sas? The user would be able to comment out the variables not in use, and it would run different querys depending on which variables were in use. Thanks! 

6 REPLIES 6
PaigeMiller
Diamond | Level 26

You can do this with macro statements. If you have SAS 9.4 TS1M5 or later

 

%if &var3^=  %then %do;
          **proc sql code here**
%end;
%else %if &var5^=  %then %do;
          **different proc sql code here** 
%end;

 If you are using earlier versions of SAS, you would have to include the above code in a macro.

--
Paige Miller
mancel3
Calcite | Level 5
The ^= syntax isnt working for me for some reason, and when I try to use %symexist(var1) it is returning true seemingly no matter what I do (if i comment out or delete the %let definition it still runs the code using that variable somehow. I haven have a delete all work statement at the top and it continues to do that) Any ideas?
PaigeMiller
Diamond | Level 26

"isn't working for me"

 

This gives us no information on how to move forward or help you. (Also, my code did not require %symexist, so I know you are not using my code)

 

Show us your code. Show us the LOG (all of the log, not just the ERROR messages) 

--
Paige Miller
ballardw
Super User

@mancel3 wrote:
The ^= syntax isnt working for me for some reason, and when I try to use %symexist(var1) it is returning true seemingly no matter what I do (if i comment out or delete the %let definition it still runs the code using that variable somehow. I haven have a delete all work statement at the top and it continues to do that) Any ideas?

%symexist will return "true" if the macro variable was created anytime during the SAS session ifthe variable was made as global macro variable such as the %let statements you show.

 

Show the entire syntax and notes from the log. There wasn't any indications that %symexist was relevant to the problem you discuss.

ChrisNZ
Tourmaline | Level 20

%symexist will return "true" if the macro variable was created anytime during the SAS

and hasn't been %SYMDEL eted. Just to clarify for the OP.

ChrisNZ
Tourmaline | Level 20

> If you are using earlier versions of SAS, you would have to include the above code in a macro.

If you use an older version of SAS, here's another way that works in open code:

 

  %sysfunc(ifc( &var3^=, %str( **proc sql code here**           )
, %sysfunc(ifc( &var5^=, %str( **different proc sql code here** )
, )) ))         

 

 

 

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
  • 857 views
  • 7 likes
  • 4 in conversation