BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayin
Quartz | Level 8
/* data have */
Class Time_0 Time_1 Time_2
  A      1     2      4
  B      0     3      9
  C      3     6      0

/* data want */
Class _01 _12
  A    2    2
  B    1    3
  C    2    0

 

 Rule 1: In data 'want', column '_01' = column 'Time_1' / 'Time_0' from data 'have'.

               '_12' = 'Time_2' / 'Time_1'.

               ....

Rule 2: If divisor = 0 then result = 1.

 

Because my actual data have more than 100 'time' columns: Time_0-Time_&Num  (Num is a global macro variable), would prefer using a loop rather than hardcode it.

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The naming of the new columns might be a little different here.  The name is based on the numerator (but not the denominator):

 

data want;

set have;

array times {0:&num} time_0 - time_#

array ratios {&num} _001 - _#

do i=1 to #

   if times{i-1} = 0 then ratios{i)=0;

   else ratios{i} = times{i} / times{i-1};

end;

drop i;

run;

 

If you would like, you can also drop all the TIME variables as well.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

The naming of the new columns might be a little different here.  The name is based on the numerator (but not the denominator):

 

data want;

set have;

array times {0:&num} time_0 - time_#

array ratios {&num} _001 - _#

do i=1 to #

   if times{i-1} = 0 then ratios{i)=0;

   else ratios{i} = times{i} / times{i-1};

end;

drop i;

run;

 

If you would like, you can also drop all the TIME variables as well.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are mixing up usage here.  Columns names are for identification in the programming, they are not meant to be used as data.  

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1668 views
  • 4 likes
  • 3 in conversation