Adding Based on File Name

Reply
Frequent Contributor
Posts: 80

Adding Based on File Name

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

 

PROC Star
Posts: 834

Re: Adding Based on File Name

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;
Frequent Contributor
Posts: 80

Re: Adding Based on File Name

Posted in reply to novinosrin

thanks...suppose there can be any number of variables...is there a way to do this in the general case?

Super User
Posts: 21,572

Re: Adding Based on File Name

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. 

PROC Star
Posts: 834

Re: Adding Based on File Name

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!!!!

Super User
Posts: 6,011

Re: Adding Based on File Name

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.

Ask a Question
Discussion stats
  • 5 replies
  • 98 views
  • 0 likes
  • 4 in conversation