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

Hi,

 

I've created a list of macro variables using the following:

%macro test;
data want1;
	set sashelp.class;
	suffix=put(_n_,5.);

	array xxx{*} _numeric_;
	do i =1 to dim(xxx);
		call symput(cats(vname(xxx[i]),"_", suffix),xxx[i]);
	end;

	array yyy{*} $ _character_;
	do i =1 to dim(yyy);
		call symput (cats(vname(yyy[i]), "_", suffix),yyy[i]);
	end;
run;
proc sql noprint;
	select count(*)
		into :nobs
	from want1;
	quit;
%do j = 1 %to &nobs;
%let current_name = &name_&j;
%put &current_name;
%end;
%mend test;
%test;

So I have macro variables name_1, name_10, ...

The %put statement does output the correct names in the log, but I receive the following error after each statement:

WARNING: Apparent symbolic reference NAME_ not resolved.

What do I need to do to resolve this?

 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@jl1005 wrote:

The dataset is directly from the sas help directory and is the class dataset.

 

 

...

 

I want to be able to create however many macro variables for each name in the name column, in this case, 19 variables called name_1, name_2, ..., name_19. I then want to create a separate macro variable called current_name and assign some name_i to it to use for a procedure. So for each iteration through the loop current_name would be current_name = name_1, current_name = name_2, ... current_name = name_19.

 

The program assigns the variables correctly and prints them correctly in the log, it just prints the error that the symbolic reference name_ not resolved. 


So if you want to make a series of macro variables from the NAME varaible then you want something like this:

 

data _null_;
  set sashelp.class ;
  call symputx(catx('_','name',_n_),name);
run;

If you want to refer to them then you need to us this syntax.

 

 

%put &name_1 ;

So then your %DO loop need to look more like:

%do counter=1 to 3 ;
  %put &&name_&counter ;
%end;

Or if you want to store the derived macro variable name then you need to use

%do counter=1 to 3 ;
  %let derived_name = name_&counter ;
  %put &&&derived_name ;
%end;

Notice that there is no extra & in the %LET statement.  That is what is causing your error message.

Also notice the triple & used to find the value of a macro variable that is named by the value of anothe rmacro variable.

On the first pass the macro processor will convert '&&' into '&' and then convert '&derived_name' into 'name_1'. Then a second pass will convert '&name_1' into the value stored in the NAME_1 macro variable.

 

But WHY???????  What possible value can get by taking data out of dataset and stuffing into character strings stored in macro variables?

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Do you know the names of the dataset variables? Because you are using the VNAME() function to retrieve the name of the element in the array and use that as PART of the macro variable names that the CALL SYMPUT() function is generating.

 

Also what value did you assign the macro variable NAME_ ?

 

Please provide a small sample data set (in the form of a data step that other's can run) and what macro variables you want to generate from that dataset.

 

Also WHY do think you need to generate so many macro variables?  What are you going to with all of them?

jl1005
Obsidian | Level 7

The dataset is directly from the sas help directory and is the class dataset.

 

The names I'm concerned about are under the variable name:

macro snip.JPG

 

This is just a smaller scale example of what I want to do with a much larger dataset, but it follows the same principles. 

 

I want to be able to create however many macro variables for each name in the name column, in this case, 19 variables called name_1, name_2, ..., name_19. I then want to create a separate macro variable called current_name and assign some name_i to it to use for a procedure. So for each iteration through the loop current_name would be current_name = name_1, current_name = name_2, ... current_name = name_19.

 

The program assigns the variables correctly and prints them correctly in the log, it just prints the error that the symbolic reference name_ not resolved. 

Tom
Super User Tom
Super User

@jl1005 wrote:

The dataset is directly from the sas help directory and is the class dataset.

 

 

...

 

I want to be able to create however many macro variables for each name in the name column, in this case, 19 variables called name_1, name_2, ..., name_19. I then want to create a separate macro variable called current_name and assign some name_i to it to use for a procedure. So for each iteration through the loop current_name would be current_name = name_1, current_name = name_2, ... current_name = name_19.

 

The program assigns the variables correctly and prints them correctly in the log, it just prints the error that the symbolic reference name_ not resolved. 


So if you want to make a series of macro variables from the NAME varaible then you want something like this:

 

data _null_;
  set sashelp.class ;
  call symputx(catx('_','name',_n_),name);
run;

If you want to refer to them then you need to us this syntax.

 

 

%put &name_1 ;

So then your %DO loop need to look more like:

%do counter=1 to 3 ;
  %put &&name_&counter ;
%end;

Or if you want to store the derived macro variable name then you need to use

%do counter=1 to 3 ;
  %let derived_name = name_&counter ;
  %put &&&derived_name ;
%end;

Notice that there is no extra & in the %LET statement.  That is what is causing your error message.

Also notice the triple & used to find the value of a macro variable that is named by the value of anothe rmacro variable.

On the first pass the macro processor will convert '&&' into '&' and then convert '&derived_name' into 'name_1'. Then a second pass will convert '&name_1' into the value stored in the NAME_1 macro variable.

 

But WHY???????  What possible value can get by taking data out of dataset and stuffing into character strings stored in macro variables?

 

jl1005
Obsidian | Level 7

Tom, removing the & and adding &&& solved it; thank you.

The reasoning behind this is that I need this for a project on simulating baseball games. Instead of going into the code each time to change the values of 30-some variables, I want to read in a csv that contains values for the variables, where each row contains parameters for each game. So for example, team_1 would be the team playing for the first game, team_2 would be playing in the second, and so on. So the simulation will loop however many rows are in the csv and change the variables for each iteration, thus making it possible to simulate multiple games at once, instead of one at a time, hopefully. 

Reeza
Super User
proc sql noprint;

select name into :name1-
from sashelp.class;
quit;

I cannot see how macro logic would be faster in any method of simulation but have at it...

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
  • 5 replies
  • 967 views
  • 0 likes
  • 3 in conversation