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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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