07-21-2017 03:25 PM
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..
07-21-2017 03:33 PM
The statements execute in a different order than you are expecting. Your code adds four statements to the DATA step, creating:
call symput(cats('var',1), a);
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.
07-21-2017 03:49 PM - edited 07-21-2017 03:49 PM
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...
Need further help from the community? Please ask a new question.