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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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