BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

I have a situation whereby i need to calculate a value for each row, relative to the first row.

ie. if i have the following table

a b c
d e f
g h i

then i wish to calculate

a b c/c
d e f/c
g h i/c

I have been able to do this using data steps and tabulates however because i repeat the calculation many times it is very time consuming and computationally demanding.

I have also been able to calculate the value for consecutive rows, but cannot keep dividing by the value c.

Any help would be greatly appreciated.
Chis
3 REPLIES 3
Tim_SAS
Barite | Level 11
Does this work for you? It assumes that z will never be missing.

[pre]
data;
input x y z;
datalines;
1 2 3
4 5 6
7 8 9
;;;;
proc report nowd;
col x y z z2;
define x--y / display;
define z / display noprint;
define z2 / computed;
compute z2;
if keep = . then keep = z;
z2 = z / keep;
endcomp;
run;[/pre]

Produces
[pre]
x y z2
1 2 1
4 5 2
7 8 3
[/pre]
deleted_user
Not applicable
That's a fantastic solution, excellent bit of code.

Thanks,
Chris
deleted_user
Not applicable
Hi again,

I've used the bit of code supplied ealier and it has worked well, however i have been unable to adapt it to deal with the following problem:

If i have a report that is grouped by month, i need the relative figure to be figured out independantly for each month.

ie. if i have

month x y z

Jan 1 2 3
Jan 4 5 6
Jan 7 8 9
Feb a d c
Feb d e f
Feb h i j

I need to calculate


Jan 1 2 3 3/3
Jan 4 5 6 6/3
Jan 7 8 9 9/3
Feb a b c c/c
Feb d e f f/c
Feb g h i i/c

Any help would be greatly appreciated.

Thanks,
Chris

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1285 views
  • 0 likes
  • 2 in conversation