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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.