turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- How to sum to certain weeks horizontally in a row

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-08-2015 04:24 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Maria_Sissel

05-08-2015 04:31 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Maria_Sissel

05-08-2015 04:49 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

05-08-2015 05:42 AM

There you go reading the whole question again