BookmarkSubscribeRSS Feed
Crysis85
Obsidian | Level 7

So, I'm writing a macro that at a certain point must save the content of a record variable on a macro variable with incrementing name depending on the position of the table. This is a fragment

		%do i=1 %to 7;
			IF _n_ GT (4095*&i-4095) AND _n_ LT (4095*&i) THEN
				DO;
					key&i.=trim(key&i.)!!' '!!trim(left(m));
					IF _n_ EQ (4095*&i-1) THEN
					DO;
						CALL SYMPUT('key'||LEFT(&i), trim(key&i));
						DROP key&i;
						%PUT &&key&i;
					END;
				end;
		%end;

I want to save the the macro variable called key1 to key6 to the value of the  variable key1 to key6 at intervals of 4095.

I know this program is conceptually wrong, terrible and makes me sick in the stomach, but I'm not permitted to redesign it so I have to solve this problem.

The log says "WARNING: Apparent symbolic reference KEY1 not resolved.

Thanks

 

4 REPLIES 4
Loko
Barite | Level 11

Hello,

 

I guess the message comes from the line

%PUT &&key&i;

Try commenting it and see if it works.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This code is invalid as its stands, where is the rest of it.  You are mixing Base SAS statements in with Macro statements, you are using macro variables from call symput statements - and this is about the time when things get compiled - but those macro variables have not been executed yet etc.  So think of it this way, when that is pushed through the macro compiler, the call execute has not run, so %put cannot find a macro variable call &key1.

 

And this statement "but I'm not permitted to redesign it", really shouldn't be the case, if something is wrong it should be fixed, not perpetuated.

 

This is basically a very simple dataset problem.

data _null_;
  set sashelp.cars;
  retain i;
  if _n_=1 then i=1;
  if mod(_n_,100)=0 then do;
    call symput(cat('key',put(i,1.)),model);
    i=i+1;
  end;
run;

Although why you want to create X amount of macro variables is beyond me, sounds like th ewhole process is a bit shaky.

Kurt_Bremser
Super User

The %put is dealt with by the macro processor immediately when encountered, which means before the data step is compiled. But the call symput executes during data step execution phase, so &keyX (X being 1 to 7) will never exist when the %put is executed.

 

Short: you cannot %put a macro variable before the data step that creates it with call symput() has run.

Crysis85
Obsidian | Level 7

I understand the problem. I put the %PUT (oh boy) there only to check if the macro variables were written, didn't think about the order on witch are executed, my bad

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
  • 1232 views
  • 3 likes
  • 4 in conversation