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

Hello,

How to create table like this one below. Sum first and second value and than from each value get rate. Is there anybody who know code for this problem?

 

My data

 

dateValueED
2016128284200
20161262101
2016123159510
2016126411

 

Want data

 

dateValueEDSumRate
2016128284200834630,74%
20161262101  
2016123159510316590,20%
2016126411  
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data have;
input date $ value e d;
cards;
201612 82842 0 0
201612 621 0 1
201612 31595 1 0
201612 64 1 1
;
run;

data want;
merge
  have
  have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
  sum = value + _value;
  rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;

proc print data=want noobs;
run;

Result:

 date     value    e    d     sum        rate

201612    82842    0    0    83463     0.74% 
201612      621    0    1        .      .    
201612    31595    1    0    31659     0.20% 
201612       64    1    1        .      .    

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User
data want;
merge
  have
  have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
  sum = value + _value;
  rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;

Edit: added a closing bracket in the merge statement, to correct a syntax error.

jopo12345678
Calcite | Level 5

This is result of this code...it doesnt work properly.

dateValueEDSumRate
20161262100124250.0%
2016123159501..   
201612641012850.0%
2016126411..   
Kurt_Bremser
Super User
data have;
input date $ value e d;
cards;
201612 82842 0 0
201612 621 0 1
201612 31595 1 0
201612 64 1 1
;
run;

data want;
merge
  have
  have (firstobs=2 rename=(e=_e d=_d date=_date value=_value))
;
if d = 0 and _date = date and _e = e
then do;
  sum = value + _value;
  rate = _value / sum;
end;
format rate percent7.2;
keep date value e d sum rate;
run;

proc print data=want noobs;
run;

Result:

 date     value    e    d     sum        rate

201612    82842    0    0    83463     0.74% 
201612      621    0    1        .      .    
201612    31595    1    0    31659     0.20% 
201612       64    1    1        .      .    

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 889 views
  • 0 likes
  • 2 in conversation