DATA Step, Macro, Functions and more

Uninitialized var in macro

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Uninitialized var in macro

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);


Accepted Solutions
Solution
‎06-11-2015 01:48 PM
Super User
Super User
Posts: 6,500

Re: Uninitialized var in macro

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


All Replies
Solution
‎06-11-2015 01:48 PM
Super User
Super User
Posts: 6,500

Re: Uninitialized var in macro

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.

New Contributor
Posts: 2

Re: Uninitialized var in macro

Thank you Tom!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 226 views
  • 0 likes
  • 2 in conversation