BookmarkSubscribeRSS Feed
Maria_Sissel
Calcite | Level 5

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Reeza
Super User

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There you go reading the whole question again Smiley Happy

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 2067 views
  • 0 likes
  • 3 in conversation