Using a Macro Variable inside a Datastep

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Using a Macro Variable inside a Datastep

Requirement here is to have a global variable (var1) with a default value (1) that may be manipulated (conditional new value = Y) under 1st datastep (data1) and then to use it under 2nd datastep (_null_) to perform some other action conditionally.

 

Problem is, under the _null_dataset though var1 is carrying correct value (Y) from data1, it's not satisfying the IF-condition, rather going to ELSE-part.

 

Here is the code:

 

%global var1;         
%let var1 = 1;        
                      
data data1;           
var2 = 3;             
if var2 = 3 then do;  
%let var1 = Y;        
end;                  
run;                  
                      
proc print data=data1;
run;                  
                      
%put &var1;           
                      
data _null_;          
varz= symget('var1');
file OUTF;            
put @1 'start';       
if varz = Y then do;  
   put @1 'yes';     
end;                  
else do;              
   put @1 'no';       
end;                  
run;                 

 

My expectation is that it should print "yes" from IF-part, not the "no" from ELSE-part.


Accepted Solutions
Solution
‎10-07-2016 05:21 PM
Super User
Posts: 1,253

Re: Using a Macro Variable inside a Datastep

%LET is a declaration statement and can't be used in a data step as you did it.

Instead you can assign a new value to a macro variable by:

 

IF ..condition.. then

    call symput('VAR1' , 'Y');

 

on next step you are using call symget which is the right thing to do;

View solution in original post


All Replies
Solution
‎10-07-2016 05:21 PM
Super User
Posts: 1,253

Re: Using a Macro Variable inside a Datastep

%LET is a declaration statement and can't be used in a data step as you did it.

Instead you can assign a new value to a macro variable by:

 

IF ..condition.. then

    call symput('VAR1' , 'Y');

 

on next step you are using call symget which is the right thing to do;

New Contributor
Posts: 2

Re: Using a Macro Variable inside a Datastep

@Shmuel Thanks much!

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 294 views
  • 1 like
  • 2 in conversation