BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

Is this the %VARCOND() macro you are trying to call in the middle of a %IF statement?

13         %macro varcond;
14         %let dsn=AE;
15         	data _temp_;
16         			%do i =1 %to %sysfunc(countw(&variables.));
17                			 %let var=%scan(&variables.,&i);
18         				varcond= "%"||"varexist(sdtmcro.&dsn,&var)";
19                			 output;
20         			 %end;
21         			 domain="&dsn";
22         	run;
23         
24         proc sort data=_temp_;
25         by domain ;
26         run;
27         
28         data _temp_;
29         length varcondn $500.;
30         do until(last.domain );
31         set _temp_;
32         by domain ;
33         varcondn=catx(' and ',varcondn,varcond);
34         end;
35         run;
36         
37         proc sql;
38         select varcondn into : varcondn
39         from _temp_;
40         quit;
41         
42         %put &varcondn.;
43         
44         %mend varcond;

It is generating many many SAS statements. You cannot call that macro in the middle of another statement. 

If you write 

%if %varcond(lb) %then .... 

It will generate code into the middle of your %IF statement.

%if data _tmp_; ...

It looks like that macro is intended to set a macro variable named VARCONDN.

So first create the macro variable. Then call the macro. Then test the macro variable.

%let varcondn=0 ;
%varcond;
%if (&varcondn) %then ....
petlove
Obsidian | Level 7

Yes. This is the macro I am calling in another program.

 

What will be the best approach to create macro variable which holds the "string" e.g. "%varexist(sdtmcro.&dsn,var1) and %varexist(sdtmcro.&dsn,var2)" and it can be recalled in another program in IF statement?

 

NOTE:

VAR1 and VAR2 can be any variables which are defined in some other macro variable &VARIABLES. This is not constant macro variable, it changes with dataset name.

Tom
Super User Tom
Super User

@petlove wrote:

What will be the best approach to create macro variable which holds the "string" e.g. "%varexist(sdtmcro.&dsn,var1) and %varexist(sdtmcro.&dsn,var2)" and it can be recalled in another program in IF statement?

 

NOTE:

VAR1 and VAR2 can be any variables which are defined in some other macro variable &VARIABLES. This is not constant macro variable, it changes with dataset name.


Here is a macro that you can in a "functional" mode. That is it emits only part of a statement. So you could use it inside an IF, %IF or assignment statement.

%macro varcond(dsn,varlist);
%local i sep ;
%do i=1 %to %sysfunc(countw(&varlist));
  &sep  %varexist(&dsn,%scan(&varlist,&i))
  %let sep=and;
%end;
%mend varcond;

So you might use it this way.  

80   options mprint;
81   %let mydataset=SASHELP.CLASS;
82   %let variables=AGE GENDER ;
83   data _null_;
84     if %varcond(&mydataset,&variables) then put "&mydataset has all of the variables: &variables"
84 ! ;
MPRINT(VAREXIST):   3
MPRINT(VARCOND):   and
MPRINT(VAREXIST):   0
85     else put "&mydataset is missing one or more of the variables: &variables";
86   run;

SASHELP.CLASS is missing one or more of the variables: AGE GENDER
NOTE: DATA statement used (Total process time):
      real time           0.26 seconds
      cpu time            0.01 seconds


87
88   %let variables=AGE SEX ;
89   data _null_;
90     if %varcond(&mydataset,&variables) then put "&mydataset has all of the variables: &variables"
90 ! ;
MPRINT(VAREXIST):   3
MPRINT(VARCOND):   and
MPRINT(VAREXIST):   2
91     else put "&mydataset is missing one or more of the variables: &variables";
92   run;

SASHELP.CLASS has all of the variables: AGE SEX
NOTE: DATA statement used (Total process time):
      real time           0.34 seconds
      cpu time            0.00 seconds

 

petlove
Obsidian | Level 7

Thank you  so much Tom for your suggestion. The approach you mentioned is working in my program.

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 3291 views
  • 0 likes
  • 5 in conversation