suppose I have the following variables that are organized like:
CYOW_ARR_NH
CYOW_ARR_H
CYOW_ARR_TOT
CYOW_DEP_NH
CYOW_DEP_H
CYOW_DEP_TOT
I would like to create a sum based on file name. For example, sum up all variables that have _ARR_NH, then maybe _DEP_H. That is, the variable can have anything in the first four letters, but add based on the suffix.
Any ideas?
thanks
something like this:
data have;
array t(*) CYOW_ARR_NH
CYOW_ARR_H
CYOW_ARR_TOT
CYOW_DEP_NH
CYOW_DEP_H
CYOW_DEP_TOT (6*1)
;
run;
proc transpose data=have out=_have;
run;
proc sql;
create table want as
select _NAME_, sum(col1) as sum
from _have
group by _name_;
quit;
thanks...suppose there can be any number of variables...is there a way to do this in the general case?
A dynamic approach is to transpose, generate a new variable that allows you to map the variables to the correct group and then summarize. In SQL you can do the last two steps at once.
yes, the data have is just an example i took it from you. But the grouping is achieved by proc transpose regardless. So don't take notice of data have. Just the approach is transpose and group by. simple as that!!!!
A more generic version ...
proc transpose data=have out=_have;
var _numeric_;
run;
data want;
set _have;
prefix = scan(_name_, 1, '_');
suffix = substr(_name_, 1 + length(prefix));
run;
Now your data set is ready to sum up COL1 ... you can group by SUFFIX or even PREFIX if that makes sense.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.