Discussion:
The SUM function
(too old to reply)
j***@gmail.com
2005-10-26 13:04:43 UTC
Permalink
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
Terjeson, Mark (IM&R)
2005-10-26 14:45:17 UTC
Permalink
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
s***@yahoo.com
2005-10-26 15:20:38 UTC
Permalink
I think it is not. But you could define a array with whatever variables
referenced, and sum over that array.

HTH.

data t1;
retain a_suffix b_suffix c_suffix 5;
output;
run;

data _null_;
set t1;
array abc(*) a_suffix b_suffix c_suffix;
sabc=sum(of abc(*));
put 'sabc= ' sabc;
run;

Loading...