BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Don22
Calcite | Level 5

Dear all,

I am a new user of SAS and struggling to find a solution for below case.

 

I want to calculate  

1. Sum of values in column "case" corresponding to day 1-4 in column "Day"  

2. Sum of values in column "case" corresponding to day 5-9 in column "Day" etc

Day1Case
17
20
35
47
512
643
76
816
925
1019
1113
1238
1339
1472
1524
1619
1745
1845
1946
2045

 

I have more than 200 rows and need to try different options by day period (1-5 days, 6-10 days or 1-7 days, 8-11 days etc)

I appreciate if you could teach me the SAS syntax to solve this.

 

Thanks

Don22 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If there is no pattern to the intervals you will have to type them out.

If you want overlapping intervals then use a multilabel format and a procedure that knows how to use them.

data have;
  input Day1 Case ;
cards;
1 7
2 0
3 5
4 7
5 12
6 43
7 6
8 16
9 25
10 19
11 13
12 38
13 39
14 72
15 24
16 19
17 45
18 45
19 46
20 45
;

proc format ;
  value days (multilabel)
1-4 =' 1- 4'
5-9 =' 5- 9'
10-19='10-19'
11-17='11-17'
18-20='18-20'
;
run;

proc means data=have sum;
  class day1 / mlf ;
  format day1 days.;
  var case;
run;
The MEANS Procedure

 Analysis Variable : Case

           N
Day1     Obs             Sum
----------------------------
 1- 4      4      19.0000000

 5- 9      5     102.0000000

10-19     10     360.0000000

11-17      7     250.0000000

18-20      3     136.0000000
----------------------------

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

I want to calculate

1. Sum of values in column "case" corresponding to day 1-4 in column "Day"

2. Sum of values in column "case" corresponding to day 5-9 in column "Day" etc

 

Okay, that would be easy if we stopped at day number 9. But what about the rest of the days, how do we group them? I don't see a pattern. What pattern of grouping days together do we follow for computing sums?

 

By the way, in order to display a pattern (at least to me) you need to show at least the first three elements, and possibly more than 3 elements, of the pattern.

--
Paige Miller
Tom
Super User Tom
Super User

So you want to convert DAY into 5 day groups or 7 day groups etc?

Sounds like a task for simple arithmetic.

data want;
  set have;
  array group[5:10] group5-group10;
  do days=5 to 10;
    group[days]=ceil(day1/days);
  end;
run;

proc means data=want min max sum;
  ways 1;
  class group10-group5 ;
  var case;
run;
Don22
Calcite | Level 5

Dear Tom,

Thank you for your response. I tried the syntax. But it is not working saying  "A class or frequency variable is missing on every observation". 

I'd like to have the following output, for example, 

Interval              total cases
 day 1-  419
day 5-9102
day 1019
day 11-17250
day 18-20136

 

Do you have any suggestions?

Thanks again.

Tom
Super User Tom
Super User

If there is no pattern to the intervals you will have to type them out.

If you want overlapping intervals then use a multilabel format and a procedure that knows how to use them.

data have;
  input Day1 Case ;
cards;
1 7
2 0
3 5
4 7
5 12
6 43
7 6
8 16
9 25
10 19
11 13
12 38
13 39
14 72
15 24
16 19
17 45
18 45
19 46
20 45
;

proc format ;
  value days (multilabel)
1-4 =' 1- 4'
5-9 =' 5- 9'
10-19='10-19'
11-17='11-17'
18-20='18-20'
;
run;

proc means data=have sum;
  class day1 / mlf ;
  format day1 days.;
  var case;
run;
The MEANS Procedure

 Analysis Variable : Case

           N
Day1     Obs             Sum
----------------------------
 1- 4      4      19.0000000

 5- 9      5     102.0000000

10-19     10     360.0000000

11-17      7     250.0000000

18-20      3     136.0000000
----------------------------
Don22
Calcite | Level 5

Thank you very much. That's exactly I want. 

Thanks again

PGStats
Opal | Level 21

You can do this in a single step with proc SQL :

data have;
input Day1 Case;
datalines;
1   7
2   0
3   5
4   7
5   12
6   43
7   6
8   16
9   25
10  19
11  13
12  38
13  39
14  72
15  24
16  19
17  45
18  45
19  46
20  45
;

%let interval = 5;

proc sql;
/* create table want as */
select 
    1 + &interval.*floor((day1-1)/&interval.) as fromDay,
    &interval.*ceil((day1)/&interval.) as toDay,
    catx("-", calculated fromDay, calculated toDay) as label,
    sum('Case'n) as intervalCases
from have
group by fromDay, toDay, label;
quit

PGStats_0-1657402197532.png

Note: The syntax 'Case'n tells SAS that Case is a variable name and not the reserved SQL word.

PG
Don22
Calcite | Level 5

Thank you very much, could you please advise me how this syntax can be if I need to choose unequal interval? For example, I want the following output.

 

Intervaltotal  cases
day 1-419
day 5-9102
day 1019
day 11-17250
day 18-20136

Thanks again.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 1175 views
  • 6 likes
  • 4 in conversation