BookmarkSubscribeRSS Feed
VarmaSekhar
Calcite | Level 5

Hello All

 

I have input data looking like this.

 

ID VAL

1  8

1  9

1  16

2  10

2  17

2  12

3  11

3  11

4  12

4   24

 

DESIRED OUTPUT:

 

ID VAL NB

1  8      1        

1  9       1.125 

1  16     2        

2  10     1        

2  17     1.7

2  12      1.2

3  11      1

3  55       5

4  12       1

4   24      2

 

In other words, for the first variable of ID, retain VAL and use it to divide with the VAL value until the next first ID variable.

For ID of 1, retain the first VAL (8) and use it to divide subsequent VAL values and create NB. In this case, (8/8=1), (9/8=1.125), (16/8=2) etc.

 

Thanks

 

3 REPLIES 3
stat_sas
Ammonite | Level 13

data want;
set have;
by id;
retain denominator;
if first.id then denominator=val;
nb=val/denominator;
run;

LinusH
Tourmaline | Level 20

@stat_sas provided a solid solution.

But I might ask what is the business rule behind this calculation? How can you be sure that the first row contains the denominator, I can't see any other variable to specify the order, such as time or sequence no.

Or is this just a practice?

Data never sleeps
ballardw
Super User

Is there any chance that VAL is less than the first value for each ID group? What would the result look like?

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 922 views
  • 0 likes
  • 4 in conversation