BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

I have the following dataset; may I know how to get the desired output? Anyone can help? Thanks.

(rate to multiply for similar case no)

 

data test;
input case $ rate;
datalines;
110 0.5
110 0.4
111 0.8
112 0.6
112 1.2
;
run;

 

Desired output

 

Case  Rate

110    0.2

111    0.8

112    0.72

 

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

What is the logic here? How is rate calculated?

scb
Obsidian | Level 7 scb
Obsidian | Level 7

110 0.5
110 0.4
111 0.8
112 0.6
112 1.2
;
run;

 

Desired output

 

Case Rate           logics

110 0.2                0.5 x 0.4 (2 cases for 110)

111 0.8                0.8 (one case only for 111)

112 0.72              0.6 x 1.2 (2 cases for 112)

 

PeterClemmensen
Tourmaline | Level 20

Ah ok. You can do something like this. I changed case to _case, since it is a reserved keyword in PROC SQL 🙂

 

data test;
input _case $ rate;
datalines;
110 0.5
110 0.4
111 0.8
112 0.6
112 1.2
;
run;

proc sql;
    create table want as
    select _case, 
           exp(sum(log(rate))) as newrate
    from test
    group by _case;
quit;
scb
Obsidian | Level 7 scb
Obsidian | Level 7

Thank you very much.

 

May I know how is the following work, especially the exp? I tried to split to bit by bit; but confused on the exp part.

exp(sum(log(rate)))

 

PeterClemmensen
Tourmaline | Level 20

This is just a workaround to simulate a 'product function'. 

Kurt_Bremser
Super User

@scb wrote:

Thank you very much.

 

May I know how is the following work, especially the exp? I tried to split to bit by bit; but confused on the exp part.

exp(sum(log(rate)))

 


exp is the reverse of log.

From inside out:

  1. get logarithm of individual rate
  2. sum up all logarithms within a group
  3. exponentiate the result

Since summing of logarithms is equivalent to multiplying, you get a cumulative multiplication formula.

Kurt_Bremser
Super User
data test;
input case $ rate;
datalines;
110 0.5
110 0.4
111 0.8
112 0.6
112 1.2
;
run;

data want;
set test (rename=(rate=_rate));
by case;
retain rate;
if first.case then rate = 1;
rate = rate * _rate;
if last.case then output;
drop _rate;
run;
Ksharp
Super User
 

data test;
input case $ rate;
datalines;
110 0.5
110 0.4
111 0.8
112 0.6
112 1.2
;
run;

data want;
 set test;
 by case;
 retain want;
 if first.case then want=1;
 want=want*rate;
 if last.case;
 drop rate;
 run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 8 replies
  • 1110 views
  • 1 like
  • 4 in conversation