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

 I'm trying to create a variable list to pass it to a function. Right now I'm creating it manually for instance if n = 5, I have to create x1,x2,x3,x4,x5 seperated by comma, if n = 6 then I have to create x1,x2,x3,x4,x5,x6

 

%let n = 5;

%let variable = x1,x2,x3,x4,x5;

 

%let n=6;

%let variable = x1,x2,x3,x4,x5,x6;

 

my question is  how to create "variable" macro automatically depening on the n value specified? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The solution will depend a bit on how you actually get to this value of 'n' but the way you've asked the question you could go for a function style macro.

%macro create_list(basename,n);
  %local _var;
  %if &n>0 %then %let _var=&basename.1;
  %do i=2 %to &n;
    %let _var=&_var,&basename&i;
  %end;
  &_var
%mend;

%let variable=%create_list(x,6);

%put &=variable;

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

run next lines under a macro:

 

%let n=5;

%let varables=;

%do i=1 %to &n;

        %let variables = &variables.x&i ;

        %if i < &n %then %let vaiables = &variables,;

%end;

%put VAEIABLES= &variables;

Jagadishkatam
Amethyst | Level 16
%let n=5;
%let variable=x;

data have;
do i=1 to &n;
variable=cats("&variable",put(i,best.));
output;
end;
run;

proc sql noprint;
select variable into :varlist separated by ',' from have;
quit;

%put &varlist;
Thanks,
Jag
Patrick
Opal | Level 21

The solution will depend a bit on how you actually get to this value of 'n' but the way you've asked the question you could go for a function style macro.

%macro create_list(basename,n);
  %local _var;
  %if &n>0 %then %let _var=&basename.1;
  %do i=2 %to &n;
    %let _var=&_var,&basename&i;
  %end;
  &_var
%mend;

%let variable=%create_list(x,6);

%put &=variable;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"pass it to a function" - in what sense do you need a macro or macro variable for this task.  Base SAS - the programming language - provides numerous different avenues to achieve these results without resorting to %$%% code:

Arrays: 

array x{6};

 

List of paramters:

sum(of x:);

 

List between paratmers:

keep=x1--x6;

 

Etc.

 

Forecaster
Obsidian | Level 7

@RW9I'm trying to pass the macro into a FMCP function which would be later used in an optimization procedure. For example: The key here is the when defining function fdef, lets suppose I have x1,x2,x3,x4,x5,x6. none of them work except hard coding. If there is a better way to pass the &var please let me know

 

 

proc fcmp outlib=sasuser.myfuncs.mypkg;

   function fdef(&var);
   array x[&n.];
   array y[&n.,&n.] /nosym (&mat.); 

	fx = 0;
 	do j = 1 to dim(y);
		fx = fx + (y[i,j]- (x[i]*x[j]));
      	
	end;
	return(fx);
   endsub;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Seems to me like your trying to use every technology other than Base SAS.  Your given fuction can be copied into a datastep, no need to hide code behind a compiled function.  Your macro variables can be replaced with variable lists and arrays or ideally remodelling your data into a better normalised structure so that by groups can be utilised.  Also, as it looks like your are doing lots of calculations across arrays, perhaps proc iml may be the way to go.

Anyways just opinion of course.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 20615 views
  • 6 likes
  • 5 in conversation