Help using Base SAS procedures

How to sum to certain weeks horizontally in a row

Reply
New Contributor
Posts: 3

How to sum to certain weeks horizontally in a row

We have a question about how to sum horizontally in SAS.

The data exists in SAS format, and we know the names of the variables.

Our dataset contains several horizontal rows, each one representing a person.

Each column represents one week and all the week-variables are numeric (the time period is from year 1999-2001, thus the total number of columns/weeks is 52*3 = 156).

All observations contain one of the following values: “1” and “0”.

We would really appreciate if anyone would like to help us with a SAS code enabling us to sum horizontally in a row (i.e. a time period for each person of the dataset) and thereby generating a variable,

which counts the sum of week number 25 and 26 from the first week you meet the value “1”. Meaning that the code from the first time you meet “1” counts 25 and 26 weeks ahead and generates a variable, which contains the sum of those two weeks. An example is a “1” observation in week 12 in 1999, the new variable should only count 25 and 26 weeks ahead, and thereby only summing week 37 and 38 in 1999. The new variable (sum variable) can either take the value 0, 1 or 2 depending on whether the “1” value is present in zero, one week or both weeks (week 37 and 38). It doesn’t matter which values the weeks between week 1 and 25 contains (“0” or “1”).

The first observed “1” has to be placed in a week in the period year 1999-2001, but the following observations (week number 25 and 26) are also allowed to be places outside the period (e.g. in year 2002), as long as the first observation is placed in a week between year 1999-2001.

Thank you in advance.

Best regards,

Maria and Sissel

Super User
Super User
Posts: 7,413

Re: How to sum to certain weeks horizontally in a row

Well, this is untested code as no test data:

data want;

     set have;

     array col{156};  /* Assumes each of the columns is COL1, COL2, COL156 */

     result=sum(of col{*});

run;

Super User
Posts: 17,898

Re: How to sum to certain weeks horizontally in a row

Assuming all numeric variables:

data want;

     set have;

     array col{156} col1 - col156;  /* Assumes each of the columns is COL1, COL2, COL156 */

     first_1=whichn(1, of col(*));*find the first 1;

    

     if first_1+25<dim(col) then  /*make sure you're not going past the end of the data*/

          indicator=col(first_1+25) + col(first_1+26); /*add the 25th & 26th data points past first 1 */

run;

Super User
Super User
Posts: 7,413

Re: How to sum to certain weeks horizontally in a row

There you go reading the whole question again Smiley Happy

Ask a Question
Discussion stats
  • 3 replies
  • 701 views
  • 0 likes
  • 3 in conversation