Hi,
I have unexpected result from the call symput function of SAS.
I wrote this code:
data tab1; a=1; b=2; output; a=3; b=5; output; run; %macro loop; data tab2; set tab1; %do i=1 %to 2; call symput(cats('var',&i),a); call symput(cats('dem',&i),b); %end; run; %do i=1 %to 2; %put i &&var&i &&dem&i; %end; %mend; %loop;
from this code I get the result:
1 3 5
2 3 5
Instead of :
1 1 2
2 3 5
(In linux...sas doesn't recognize the macro variable &&var&i ).
I can't understand why... I need to save some variable in the macro variable... some of you has some solution and explain why I get this result? really..I can't understand..
Thanks
There's always a way. Here's one idea:
data tab2;
set tab1;
call symput(cats('var',_n_), a);
call symput(cats('dem',_n_),b);
run;
The statements execute in a different order than you are expecting. Your code adds four statements to the DATA step, creating:
data tab2;
set tab1;
call symput(cats('var',1), a);
call symput(cats('dem',1),b);
call symput(cats('var',2),a);
call symput(cats('dem',2),b);
run;
All of that occurs before the DATA step begins to run. Once all the statements are complete, the DATA step executes. So for the first observation from TAB1, all four statements execute. Then for the second observation from TAB1, all four statements execute, replacing the values assigned previously.
Thanks Astounding,
Your answer is very clear!!!
but.. if i want to save some variable of data set in the macro, what should i do?
because i haven't any idea...
Thanks.
There's always a way. Here's one idea:
data tab2;
set tab1;
call symput(cats('var',_n_), a);
call symput(cats('dem',_n_),b);
run;
Wow!!
Thanks Astounding!!!
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.
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.