turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to create dynamic suffix variable name and ass...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-19-2016 08:43 PM - edited 12-19-2016 08:44 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-19-2016 09:07 PM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-19-2016 08:56 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-19-2016 09:00 PM

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

%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

Jag

Solution

12-19-2016
09:27 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-19-2016 09:07 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

12-19-2016 09:27 PM

impressive

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-20-2016 04:50 AM

"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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-20-2016 08:30 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Forecaster

12-20-2016 08:37 AM

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.