BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Batman
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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
SASKiwi
PROC Star

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?

Batman
Quartz | Level 8

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

 

PaigeMiller
Diamond | Level 26
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
ballardw
Super User

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.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 560 views
  • 0 likes
  • 4 in conversation