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

Here is my question. I want to create several variables to store the values, where the suffix is an index, and strings are changed based on different conditions. However, the following code doesn't generate the expected output. 

 

%let var1=left;

%let var2=center;

%let var3=right;

 

%macro example;

%do i=1 %to 3;

%do j=1 %to 2;

%global list_&&var&i_&j;

data whatever;

list_&&var&i_&j=1

run;

%end;

%end;

%mend;

 

%example;

 

I want the macros resolved separately to have list_left_1, list_left_2,list_center_1,list_center_2,list_right_1,list_right_2. How can I get them? Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You need a dot to separate the macro variable name from the following text. In this case because you have a double ampersand, you need a double dot.

 

%global list_&&var&i.._&j;

 

Same thing here, and you left out a semi-colon as well

 

data whatever;
    list_&&var&i.._&j=1;
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You need a dot to separate the macro variable name from the following text. In this case because you have a double ampersand, you need a double dot.

 

%global list_&&var&i.._&j;

 

Same thing here, and you left out a semi-colon as well

 

data whatever;
    list_&&var&i.._&j=1;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @liyongkai800   Adding on to what @PaigeMiller  clearly explained, I am wondering do you really want many datasteps or the code should be

/*see the commented part*/



%let var1=left;

%let var2=center;

%let var3=right;

 
%put &var1;
/*options symbolgen;*/
%macro example;
data whatever;
%do i=1 %to 3;

%do j=1 %to 2;

%global list_&&var&i.._&j;

/*data whatever;*/

list_&&var&i.._&j=1;

/*run;*/

%end;

%end;
run;
%mend;


%example;
liyongkai800
Obsidian | Level 7
Thanks for your comments. Originally, I want to create some lists to store some vectors. But I totally agree with you if I want to store everything in a data set.
ballardw
Super User

If you are looking to add multiple variables to the data set as a single row:

%macro example;
   data whatever;
   %do i=1 %to 3;
      %do j=1 %to 2;
         list_&&var&i.._&j=1 ;
      %end;
   %end;
   run;

%mend;

It really helps to show what the desired data set should actually look like.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 688 views
  • 1 like
  • 4 in conversation