DATA Step, Macro, Functions and more

How to create dynamic suffix variable name and assign it to macro ?

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to create dynamic suffix variable name and assign it to macro ?

[ Edited ]

 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? 

 


Accepted Solutions
Solution
‎12-19-2016 09:27 PM
Respected Advisor
Posts: 3,890

Re: How to create dynamic suffix variable name and assign it to macro ?

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


All Replies
Trusted Advisor
Posts: 1,374

Re: How to create dynamic suffix variable name and assign it to macro ?

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;

Trusted Advisor
Posts: 1,128

Re: How to create dynamic suffix variable name and assign it to macro ?

%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
Solution
‎12-19-2016 09:27 PM
Respected Advisor
Posts: 3,890

Re: How to create dynamic suffix variable name and assign it to macro ?

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;
Contributor
Posts: 40

Re: How to create dynamic suffix variable name and assign it to macro ?

impressive
Super User
Super User
Posts: 7,401

Re: How to create dynamic suffix variable name and assign it to macro ?

"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 xSmiley Happy;

 

List between paratmers:

keep=x1--x6;

 

Etc.

 

Contributor
Posts: 40

Re: How to create dynamic suffix variable name and assign it to macro ?

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

 

Super User
Super User
Posts: 7,401

Re: How to create dynamic suffix variable name and assign it to macro ?

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.

☑ This topic is SOLVED.

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

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