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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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.

 

Rakeon
Quartz | Level 8

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.

Astounding
PROC Star

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;

Rakeon
Quartz | Level 8

Wow!!

Thanks Astounding!!!

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
  • 4 replies
  • 847 views
  • 1 like
  • 2 in conversation