Access Macro Variable Created From Loop

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Access Macro Variable Created From Loop

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.

 


Accepted Solutions
Solution
‎03-14-2018 10:49 AM
Super User
Super User
Posts: 7,756

Re: Access Macro Variable Created From Loop


@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


All Replies
Super User
Super User
Posts: 7,756

Re: Access Macro Variable Created From Loop

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?

Contributor
Posts: 47

Re: Access Macro Variable Created From Loop

[ Edited ]

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. 

Solution
‎03-14-2018 10:49 AM
Super User
Super User
Posts: 7,756

Re: Access Macro Variable Created From Loop


@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?

 

Contributor
Posts: 47

Re: Access Macro Variable Created From Loop

[ Edited ]

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. 

Super User
Posts: 22,530

Re: Access Macro Variable Created From Loop

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...

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 118 views
  • 0 likes
  • 3 in conversation