BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
panda
Quartz | Level 8

Hi - 

I want to write a loop to collapse my seconds level dummy variables (from 0:00:00 to 23:59:59, 86400 variables in total) to minutes level varibales(sum all seconds within one minute), right now my data looks like this:

id second1 second2 second3 ........ seconds86398 seconds86399 seconds86400

1   1            1             .                      .                              1                      1

2                                                             1                        1                      1

3   1            1             1

The ideal dataset is like this

id   min1 min2 min3 .........  min1439 min1440

1    2       0      0                          0         2

2    0       0      0                          30      45

3    32      24   39                         0       0

Without looping, I have to type the code for 1440 times:

data want;
    set have;
   min1 = sum(of second1-seconds60);
   min2 = sum(of seconds61-seconds120);
   min3 = sum(of second121-seconds180);
   ......
   min1440 = sum(of seconds86340- seconds86400);
run;

Any idea?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Rhodochrosite | Level 12

You could do this with a series of arrays and programming statements, but it is probably easiest to use the macro language.

data want;
   set have;
   %macro loop;
   %do i = 1 %to 1440;
      min&i = sum(of seconds%eval((&i-1)*60+1) - seconds%eval(&i*60));
      %end;
   %mend;
   %loop;
  run;
   

View solution in original post

2 REPLIES 2
WarrenKuhfeld
Rhodochrosite | Level 12

You could do this with a series of arrays and programming statements, but it is probably easiest to use the macro language.

data want;
   set have;
   %macro loop;
   %do i = 1 %to 1440;
      min&i = sum(of seconds%eval((&i-1)*60+1) - seconds%eval(&i*60));
      %end;
   %mend;
   %loop;
  run;
   
panda
Quartz | Level 8

That works perfectly, thanks!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 423 views
  • 1 like
  • 2 in conversation