01-24-2018 04:09 PM
suppose I have the following variables that are organized like:
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.
01-24-2018 04:20 PM
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;
01-24-2018 04:37 PM
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.
01-24-2018 04:38 PM
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!!!!
01-24-2018 04:50 PM
A more generic version ...
proc transpose data=have out=_have;
prefix = scan(_name_, 1, '_');
suffix = substr(_name_, 1 + length(prefix));
Now your data set is ready to sum up COL1 ... you can group by SUFFIX or even PREFIX if that makes sense.