BookmarkSubscribeRSS Feed
Solph
Pyrite | Level 9

Hi,

I've a datadata consisting of daily data from 0 day to 7300 day (20 years of data); so far there is no day gap between records, so the days are consecutive. I'd like to output the data for every 30 days (30th, 60th) and ideally after 360 days, I'd like to jump to 366 and start again for every 30 days until the next 360th days and jump to 731, and so on. How do I do it? If it is too complicate, I can do every 30 days (plus the first record). The data would look something like the following:

data aa; input

day amount;

datalines;

0   12  -> output

1   23

2   50

30 34  -> output

31 34

.

..

60 41 -> output

....

360 3  -> output

365 3

366 1

367 4

....

;

Thanks in advance for your help.

11 REPLIES 11
FriedEgg
SAS Employee

data _null_;

input day amount;

if mod(day,30)=0 then put (day amount) (/=);

cards;

0 12

1 23

2 50

30 34

31 34

60 41

360 3

365 3

366 1

367 4

;

run;

day=0

amount=12

day=30

amount=34

day=60

amount=41

day=360

amount=3


Solph
Pyrite | Level 9

Thanks a lot. It is a really neat way to do it. I thought about something like that (use division day/30) but didn't know how to proceed after that. I also just found the following way and it seemed to work (although it doesn't address the 365 days problem.)

data aa2 set aa;

    do i = 0 to 7300 by 30;

    if day=i then output;

    end;

drop i;

run;

Linlin
Lapis Lazuli | Level 10

Hi Solph,

Is this what you want?

data have;

do day=0 to 7300;

     output;

  end;

  run;

data want(drop=i j);

   set have;

   if day=0 then output;

      do j=0 to 19;

        do i=j*365 to j*365+360 by 30;

        if day=i and mod(day,365)ne 0  then output;

        end;

      end;

run;

Solph
Pyrite | Level 9

Thanks Linlin. I just saw your reply and tested it. Yes it is the alternative approach I was thinking about. I'll just need to understand the logic of the code. Thanks so much.

art297
Opal | Level 21

Do you have dates in the file?  Otherwise, the 365 seems arbitray/biased, as it doesn't account for leap years.

Solph
Pyrite | Level 9

Not in this file, although I could create a pseudo date variable if it helps. Unfortunately we'd have to to output for every 30 days (has to be integer), although ultimately we are reporting the data by year. It is going to be difficult to reconcile the days differences one way or another. Another way is program by month of course, if so I guess a date variable would be useful, right?

Reeza
Super User

Do you need equal intervals or can you take the last day of every month?

ieva
Pyrite | Level 9

I think you need to know the date. Otherwise you don’t know if ‘366’ is last day of a leap year or first day of next year. Other solution could be to have these day numbers starting from 1 every new year:

363

364

365

1

2

3

4

..

Howles
Quartz | Level 8

Even without the presence of an explicit date variable, it seems possible to use the INTNX function to avoid drift caused by leap years.

data have ;

do Day= 0 to 7300 ;

   output ;

   end ;

run;

data want (keep = day) ;

counter = 0 ;

do outer = 0 to 19 ;

   anniversary = intnx('year', 0, outer, 'same') ;

   do inner = 0 to ifn(outer LT 19, 330, 360) by 30 ;

      next = anniversary + inner ;

      do counter = counter to next ;

         set have ;

         end ;

      output ;

      end ;

   end ;

run ;

art297 wrote:

Do you have dates in the file?  Otherwise, the 365 seems arbitray/biased, as it doesn't account for leap years.

Ksharp
Super User
data have;
do day=0 to 7300;
  output;
  end;
run;
data want;
 set have;
 if _n_ eq 1 then do;output;return;end;
 count+1; 
 if mod(group,12)=0 and count=31 then do;count=-4;end;
 mod=mod(count,30);
 if mod=1 then do;group+1; count=1;end;
 if mod=0 and count ne 0 then output;
run;


Ksharp

Peter_C
Rhodochrosite | Level 12

sometimes refered to as a DOW loop, because it loops around a SET statement (but iirc named after presenters who first brought that idea to prominence Paul Dorfman and Ian Whitlock),

this step might do what was wanted = re-start counting after 365, 730,

I tested on the first 1200 rows of sashelp.prdsale >>>>

data thirtys ;

do row =  1 to 365  ;

   set sashelp.prdsale( obs=1200) ;

   read+1 ;

   if not mod( row, 30 ) then output ;

end ;

put read= ;

run

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
  • 11 replies
  • 1194 views
  • 0 likes
  • 9 in conversation