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?
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;
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;
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;
"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.
@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;
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.
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.
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.