underscores
underscores

Reputation: 3

SAS - code to dynamically count columns and sum each of them

I’m wondering if someone can help with a coding problem I have.

Background – I have a project that imports some files and uses the data in those files to perform projections. The contents of the files determines some aspects of the size of the output that follows. Simply, values in data loaded in drives the size and shape of the tables that follow, and this can vary.

The following code is an example of the problem.

The data loaded will have a variable year start (note wf2009, 2009 is the first year) and variable range (this example goes from 2009 to 2030, but this will vary too).

proc summary data= labeled_proj_data_hc;
class jurisdiction specialty measure;
types jurisdiction*specialty*measure;
VAR wf2009--wf2030;
output out= sum_labeled_proj_data_hc
    sum(wf2009) = y2009
    sum(wf2010) = y2010
    sum(wf2011) = y2011
    sum(wf2012) = y2012;
run;

Where I’m not sure how to proceed is:

sum(wf2009) = y2009
sum(wf2010) = y2010
sum(wf2011) = y2011
sum(wf2012) = y2012;

In the sequence of lines calling for the sum of their respective columns, how can I make this dynamic so that the start year is populated from a variable and it increments yearly until the last year which is also variable.

Has anyone solved a similar problem.

Cheers,

Upvotes: 0

Views: 2515

Answers (1)

Longfish
Longfish

Reputation: 7602

Is renaming the variables necessary? If not then you can use the : wildcard operator to access all variables that begin with 'wf', then just put SUM= in the output statement, which will preserve the original names.

So your proc summary would look like this.

proc summary data= labeled_proj_data_hc;
class jurisdiction specialty measure;
types jurisdiction*specialty*measure;
VAR wf: ;
output out= sum_labeled_proj_data_hc
    sum=;
run;

Upvotes: 1

Related Questions