1
votes

I have a dataset which includes the following two variables (it includes more, but for simplicity's sake, I'll just discuss these two): Date and Hot

Date is a SAS date variable and Hot is a binary (0,1) variable.

I'd like to read the dataset and have a count variable which counts the number of days in a row where the Hot variable is a 1.

This is an example of what my data look like:

Date          Hot

14JUN2010     0
15JUN2010     1
16JUN2010     1
17JUN2010     0
18JUN2010     1
19JUN2010     1
20JUN2010     1
21JUN2010     0

This is an example of what I would like my data to look like:

Date          Hot    HotDays

14JUN2010     0      0
15JUN2010     1      1
16JUN2010     1      2
17JUN2010     0      0
18JUN2010     1      1
19JUN2010     1      2
20JUN2010     1      3
21JUN2010     0      0

I've tried a number of different things, none of which work. I've read up on the retain statement and group by processing, but I haven't figured out how to adapt those strategies to counting sequential dates by binary variable.

Thanks for your help.

1
You should post what you've tried. Retain is definitely a good approach- what didn't work when you tried it? What's special about sequential dates here? - Joe

1 Answers

0
votes

You can take advantage of the values of HOT and process the data BY HOT NOTSORTED to detect when to reset the counter.

Sum Statement By Statement

data hot;
   input Date:date9. Hot;
   cards;
14JUN2010     0
15JUN2010     1
16JUN2010     1
17JUN2010     0
18JUN2010     1
19JUN2010     1
20JUN2010     1
21JUN2010     0
;;;;
   run;
data hotdays;
   set hot;
   by hot notsorted;
   if first.hot then hotdays=0;
   hotdays + hot;
   run;

enter image description here