BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I cannot go to all process so I will ask the question from this step only.

 

%macro mmacro;
%do j=1 %to &k.;
&Xj.
%end;
%mend;

 

I want to create a parameter that get the value:  X1,X2,X3,X4.........................

(As you can see the number of arguments are not fixed and depends on value of parameter &k.)

 

 

 

 

3 REPLIES 3
Kurt_Bremser
Super User

Do it in a data step:

%let k=5;
data _null_;
length param $100;
do j = 1 to &k;
  param = catx(',',param,'X' !! strip(put(j,best.)));
end;
call symputx('param',param);
run;
%put param=&param.;
Astounding
PROC Star

I'm not sure that this result would be useful.  But you can get it easily enough:

 

%macro mmacro;

   %local j;

   %global mylist;

   %let mylist=X1;
   %do j=2 %to &k.;
      %let mylist = &mylist,X&j.;
   %end;
%mend;

 

************** EDITED:

 

Based on some of your earlier questions, it is possible you don't want X1,X2,X3, but rather the value of the macro variables instead of the names of the macro variables.  While I agree with posters that have warned you that the commas are unnecessary in almost all cases and can cause trouble later, that's just something to keep in mind.  Here is the variation that uses the values of a set of macro variables:

 

%macro mmacro;

   %local j;

   %global mylist;

   %let mylist=&X1;
   %do j=2 %to &k.;
      %let mylist = &mylist,&&X&j.;
   %end;
%mend;

Patrick
Opal | Level 21

@Ronein

With a macro you could use the parmbuff option as documented here:

http://support.sas.com/documentation/cdl//en/mcrolref/69726/HTML/default/viewer.htm#p1nypovnwon4uyn1...

This option is rarely used and I can't really recommend to take such an approach. Anyway: Here a code sample:

%macro printz/parmbuff;
   %let num=1;
   %let dsname=%scan(&syspbuff,&num,%str(%(, %)));
   %put &=dsname;
   %do %while(&dsname ne);
      proc print data=&dsname;
      run;
      %let num=%eval(&num+1);
      %let dsname=%scan(&syspbuff,&num,%str(%(, %)));
   %end;
%mend printz;
%printz(sashelp.class, sashelp.air)

Alternatively when using a macro just pass in the string of values to a normal parameter (a macro variable) and then parse this macro variable. Just don't use a comma as delimiter as this gets already used by the macro statement to delimit parameters so you only ask for complications using the same for your value list.

Here some sample code using a blank as delimiter: 

%macro printz(ds);
   %let num=1;
   %let dsname=%scan(&ds,&num,%str( ));
   %put &=dsname;
   %do %while(&dsname ne);
      proc print data=&dsname;
      run;
      %let num=%eval(&num+1);
      %let dsname=%scan(&ds,&num,%str( ));
   %end;
%mend printz;
%printz(sashelp.class sashelp.air)

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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