BookmarkSubscribeRSS Feed
BCNAV
Quartz | Level 8

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

 

5 REPLIES 5
novinosrin
Tourmaline | Level 20

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;
BCNAV
Quartz | Level 8

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

Reeza
Super User

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. 

novinosrin
Tourmaline | Level 20

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

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 433 views
  • 0 likes
  • 4 in conversation