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

Hi,

suppose I have the following equation:  R = A*B, and B = C/D

and suppose I have the following table:

ABCD
a1b1
a2c2d2

In the first line I can directly calculate R1 = a1*b1, but in the second line I need to  R2 = a2*c2/d2,

so how is it possible to write a code that will choose the right formula depending on the available information?

Thank you

ps: in case all variables are available, choose the "straightest" method, i.e  R=A*B

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Hi,

Try this.

data have;

input A B C D;

datalines;

2 5 . .

1 . 10 5

;

data want;

set have;

R=a*coalesce(b,c/d);

run;

View solution in original post

4 REPLIES 4
stat_sas
Ammonite | Level 13

Hi,

Try this.

data have;

input A B C D;

datalines;

2 5 . .

1 . 10 5

;

data want;

set have;

R=a*coalesce(b,c/d);

run;

ilikesas
Barite | Level 11

Hi stat@sas, ran your code and it was good, thank you!

Could you please also show me how to achieve the same result using the IF THEN ELSE procedures because if my actual case will have more variables and more complex equations I guess it will be too messy to put everything into a single COALESCE?

Thank you

stat_sas
Ammonite | Level 13

Hi,

Below is an example using IF THEN ELSE statement.

data want;

set have;

if b ne . then R=a*b;

else R=a*c/d;

run;

Reeza
Super User

Since you'll end up hard coding things not necessarily. If you have multiple formulas/conditions I'd probably calculate them all, but then use a coalesce to determine which is the final one to keep. The issue then becomes how to keep your log clean, if that's a requirement.

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