Whups! missed GLOBAL statement;
This code will now work better
on a cold start!
* build same suffix arglist ;
%macro sumsfx(ds,suffix);
%global samesfx;
%* handle with or without libref ;
%if %index(&ds,.) eq 0 %then
%do;
%let _lib=work;
%let _ds=&ds;
%end;
%else
%do;
%let _lib=%scan(&ds,1,.);
%let _ds=%scan(&ds,2,.);
%end;
proc sql noprint;
select distinct name into :samesfx separated by ','
from sashelp.vcolumn
where name like "%nrstr(%%)&suffix.";
quit;
%put samesfx var list is >&samesfx<;
%mend;
data sample;
acount=1;
bcount=2;
ccount=3;
dcount=4;
dbbbbb=4;
run;
%sumsfx(sample,count); * create same suffix variable list ;
data result;
set sample;
abcdsum = sum(&samesfx);
run;
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
Terjeson, Mark (IM&R)
Sent: Wednesday, October 26, 2005 7:32 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: The SUM function
Hi Jason,
Here is one possibility that may
seem a little klunky but it works,
until ya find sumpthin mor elegant.
* build same suffix arglist ;
%macro sumsfx(ds,suffix);
%* handle with or without libref ;
%if %index(&ds,.) eq 0 %then
%do;
%let _lib=work;
%let _ds=&ds;
%end;
%else
%do;
%let _lib=%scan(&ds,1,.);
%let _ds=%scan(&ds,2,.);
%end;
proc sql noprint;
select distinct name into :samesfx separated by ','
from sashelp.vcolumn
where name like "%nrstr(%%)&suffix.";
quit;
%put samesfx var list is >&samesfx<;
%mend;
data sample;
acount=1;
bcount=2;
ccount=3;
dcount=4;
run;
%sumsfx(sample,count); * create same suffix variable list ;
data result;
set sample;
abcdsum = sum(&samesfx);
run;
Hope this is helpful.
Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group
Russell
Global Leaders in Multi-Manager Investing
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
***@GMAIL.COM
Sent: Wednesday, October 26, 2005 6:05 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: The SUM function
Hello folks,
I've seen a handy way to sum all variables with the same prefix.. e.g.
TOTAL = SUM(of qtr:);
where 'qtr' is the prefix. Is there a way to sum all variables with the
same suffix?
Thanks,
Jason