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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

%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

2 REPLIES 2
Shmuel
Garnet | Level 18

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

Arin
Fluorite | Level 6

@Shmuel Thanks much!

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
  • 1204 views
  • 1 like
  • 2 in conversation