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!!!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.