BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kec3814
Calcite | Level 5

I'm writing a macro where it creates date flags for different variables in my dataset. Some of the variables have an associated year variable (var_YYYY) and some don't. So in my call, I add another parameter to note if there is a year variable (yrvar= 1 or 2). I then wrote an if/then/do statement that is supposed to only perform the data step if yrvar is 1 or 2. But when I run the first call, even though yrvar has a value of 2, I still get the  note saying EN_&var2._YYYY is uninitialized. Why is it even performing the task in red if yrvar=2 in the call?

%macro days(var1,var2,res,yrvar);
data dates2;
set dates;

where EN_&var1. in (&res.);
if EN_&var2._MM in (1,2,3,4,5,6,7,8,9,10,11,12) then EN_&var1._MOFLAG=1; else EN_&var1._MOFLAG=0; **month y/n**;

if &yrvar. = 1 then do;
if EN_&var2._YYYY in (2010,2011,2012) then EN_&var1._YRFLAG=1; else EN_&var1._YRFLAG=0; **year y/n**;
if EN_&var1._MOFLAG=1 and EN_&var1._YRFLAG=1 then EN_&var1._SDFLAG=1;  else EN_&var1._SDFLAG=0; **start date y/n**;
end;

else if &yrvar. = 2 then do;
if EN_&var1._MOFLAG=1 then EN_&var1._SDFLAG=1;  else EN_&var1._SDFLAG=0; **start date y/n**;
end;

run;

%mend days;

%days(B9A1_YN,B9A2B,1,2);
%days(B9A6C_YN,B9A6D2,1,1);

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You wrote a DATA step IF statement.  That will prevent the data step from executing those lines of code, but NOT from the lines of code existing and causing reference to the variable names from being seen when SAS compiles the data step.

Use %IF %THEN %DO: .... %END: in your macro to have the MACRO conditionally generate the lines of code.  Then the data step will not have those lines at all.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You wrote a DATA step IF statement.  That will prevent the data step from executing those lines of code, but NOT from the lines of code existing and causing reference to the variable names from being seen when SAS compiles the data step.

Use %IF %THEN %DO: .... %END: in your macro to have the MACRO conditionally generate the lines of code.  Then the data step will not have those lines at all.

kec3814
Calcite | Level 5

Thank you Tom!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 3372 views
  • 0 likes
  • 2 in conversation