Solved
Contributor
Posts: 31

# Summing across variables above the value of a separate variable

Hi,

I am trying to sum across variables, but only if a separate variable is greater than a certain value. Here's an example:

 ID day1 day2 day3 dollar1 dollar2 dollar3 magicday 1 0 10 12 1 2 3 4 2 0 1 5 1 2 3 2

For any of the days (day1-day3) that are greater than the magicday, I want to sum their corresponding dollar values. For example, for person 1, since their magicday is 4, I want to add the dollar

values from day2 and day3 and get a total sum of \$5, and ignore the dollars from day 1. For person 2, I want only the dollar value from day3, \$3.

I actually have over 400 days to deal with, so doing this manually isn't really an option.

Thanks so much!

Laurie

Accepted Solutions
Solution
‎05-16-2013 03:11 PM
Super User
Posts: 23,683

## Re: Summing across variables above the value of a separate variable

Use a do loop and arrays.

data want;

set have;

array day(100) day1-day100;

array dollar(100) dolalr1-dollar100;

dollar_total=0;

do i=1 to 100;

if day(i)>magicday then dollar_total+dollar(i);

end;

run;

All Replies
Solution
‎05-16-2013 03:11 PM
Super User
Posts: 23,683

## Re: Summing across variables above the value of a separate variable

Use a do loop and arrays.

data want;

set have;

array day(100) day1-day100;

array dollar(100) dolalr1-dollar100;

dollar_total=0;

do i=1 to 100;

if day(i)>magicday then dollar_total+dollar(i);

end;

run;

Contributor
Posts: 31

## Re: Summing across variables above the value of a separate variable

Perfect, thanks!

🔒 This topic is solved and locked.