- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is a simplified version of what I'm trying to do. I use a dash because the actual number of variables quite large and am trying to avoid listing each one of them. Is there a way to get the macro to call up each variable in the array.
options mprint;
data sample;
x1=1;
x2=3;
x3=2;
run;
%macro test(vars);
%let selcnt=%sysfunc(countw(&vars));
%do i=1 %to &selcnt;
%let selvar=%scan(&vars,&i);
sum(&selvar) as &selvar
%if &i lt &selcnt %then %do; , %end;
%end;
%mend;
proc sql;
select
%test(x1-x3)
from sample;
quit;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The first line inside your macro with the way you called in the example is just plain wrong:
%macro dummy(vars); %let selcnt=%sysfunc(countw(&vars)); %put The number of words in &vars. is: &selcnt.; %mend; %dummy (x1-x3)
Which will show this output:
The number of words in x1-x3 is: 2
You only passed two words. To get what I think you expect you have to pass the list explicitly:
395 %dummy (x1 x2 x3) The number of words in x1 x2 x3 is: 3
Neither the macro language or SQL support "array" or "list" type syntax natively.
If you have a large number of variables where you think an array or list shortcut is useful then look to procedures that support those ideas such as Proc Means/Summary.
You might even try one of the reporting procedures:
proc tabulate data=sashelp.class; var _numeric_; table _numeric_, sum min max std ; run;
or
proc report data=sashelp.class; columns _numeric_; run;
These procedures will use any valid list: like x1 - x3 (for sequentially named variables) x1 -- abc (for adjacent variables in column order), mixtures of the two as well as individual variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there a way to get the macro to call up each variable in the array?
You have not defined an array. However, if your goal is to find all variables in data set SAMPLE and then sum them, you could do this:
data want;
set sample;
sum_all_variables=sum(of _numeric_);
run;
If that's not your goal, then state your goal in words, instead of having us trying to decipher your macro code, which might not really be doing what you want anyway.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It's near impossible to figure out what you are trying to do from what you've posted. For starters there is no such thing as a macro array. Macro is just a text processor. I suggest you post a non-macro example of what you are trying to do - is it a series of SUM statements in an SQL query?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
a simplified non-macro example would look something like
proc sql;
select sum(x1) as x1, sum(x2) as x2
from sample;
quit;
proc sql;
select sum(y1) as y1, sum(y2) as y2,....sum(yn) as yn
from sample;
quit;
etc, etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
proc summary data=have;
var _numeric_;
output out=want sum=;
run;
Please, @Batman , as requested above, help us out and explain in words (not code) what you want.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The first line inside your macro with the way you called in the example is just plain wrong:
%macro dummy(vars); %let selcnt=%sysfunc(countw(&vars)); %put The number of words in &vars. is: &selcnt.; %mend; %dummy (x1-x3)
Which will show this output:
The number of words in x1-x3 is: 2
You only passed two words. To get what I think you expect you have to pass the list explicitly:
395 %dummy (x1 x2 x3) The number of words in x1 x2 x3 is: 3
Neither the macro language or SQL support "array" or "list" type syntax natively.
If you have a large number of variables where you think an array or list shortcut is useful then look to procedures that support those ideas such as Proc Means/Summary.
You might even try one of the reporting procedures:
proc tabulate data=sashelp.class; var _numeric_; table _numeric_, sum min max std ; run;
or
proc report data=sashelp.class; columns _numeric_; run;
These procedures will use any valid list: like x1 - x3 (for sequentially named variables) x1 -- abc (for adjacent variables in column order), mixtures of the two as well as individual variables.