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

Hello, I wan't to calculate RATIO1 and RATIO2

 

PRODUCTQUANTILEVALUE_QUANTILERATIO1RATIO2
P15%24(60-24)/24(300-60)/60
P150%60--
P195%300--
P25%1(400-1)/1(500-400)/400
P250%400--
P295%500--
P35%300(450-300)/300(600-450)/450
P350%450--
P395%600--
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. Then do something like this

 

data have;
input PRODUCT $ 1-2 QUANTILE $ 4-6 VALUE_QUANTILE;
datalines;
P1 5%  24  
P1 50% 60  
P1 95% 300 
P2 5%  1   
P2 50% 400 
P2 95% 500 
P3 5%  300 
P3 50% 450 
P3 95% 600 
;

data want(drop=v2 v3);
   merge have
         have(firstobs=2 rename=VALUE_QUANTILE=v2 keep=VALUE_QUANTILE)
         have(firstobs=3 rename=VALUE_QUANTILE=v3 keep=VALUE_QUANTILE);
   if quantile = '5%' then do;
      ratio1 = (v2 - VALUE_QUANTILE) / VALUE_QUANTILE;
      ratio2 = (v3 - v2) / v2;
   end;
run;

Result:

 

PRODUCT QUANTILE VALUE_QUANTILE ratio1 ratio2 
P1      5%       24             1.5    4.00000 
P1      50%      60             .      . 
P1      95%      300            .      . 
P2      5%       1              399.0  0.25000 
P2      50%      400            .      . 
P2      95%      500            .      . 
P3      5%       300            0.5    0.33333 
P3      50%      450            .      . 
P3      95%      600            .      . 

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Is this for reporting purposes or do you want a SAS data set like this?

mazouz
Calcite | Level 5

I wan't solution by using step data or proc sql

PeterClemmensen
Tourmaline | Level 20

Is your data actually structured like this? 3 obs for each id?

mazouz
Calcite | Level 5

Yes the number of obs in each id is constant

PeterClemmensen
Tourmaline | Level 20

Ok. Then do something like this

 

data have;
input PRODUCT $ 1-2 QUANTILE $ 4-6 VALUE_QUANTILE;
datalines;
P1 5%  24  
P1 50% 60  
P1 95% 300 
P2 5%  1   
P2 50% 400 
P2 95% 500 
P3 5%  300 
P3 50% 450 
P3 95% 600 
;

data want(drop=v2 v3);
   merge have
         have(firstobs=2 rename=VALUE_QUANTILE=v2 keep=VALUE_QUANTILE)
         have(firstobs=3 rename=VALUE_QUANTILE=v3 keep=VALUE_QUANTILE);
   if quantile = '5%' then do;
      ratio1 = (v2 - VALUE_QUANTILE) / VALUE_QUANTILE;
      ratio2 = (v3 - v2) / v2;
   end;
run;

Result:

 

PRODUCT QUANTILE VALUE_QUANTILE ratio1 ratio2 
P1      5%       24             1.5    4.00000 
P1      50%      60             .      . 
P1      95%      300            .      . 
P2      5%       1              399.0  0.25000 
P2      50%      400            .      . 
P2      95%      500            .      . 
P3      5%       300            0.5    0.33333 
P3      50%      450            .      . 
P3      95%      600            .      . 
mazouz
Calcite | Level 5
Thank you Draycut
Kurt_Bremser
Super User
data want;
count = 0;
do until (last.product);
  set have;
  by product;
  count + 1;
  if count = 2 then secval = value_quantile;
end;
lastval = value_quantile;
do until (last.product);
  set have;
  by product;
  if first.product then do;
    ratio1 = (secval - value_quantile) / value_quantile;
    ratio2 = (lastval - secval) / secval;
  end;
  else do;
    ratio1 = .;
    ratio2 = .;
  end;
  output;
end;
drop count secval lastval;
run;
  

Untested, for lack of usable data; please post example data in a data step with datalines.

mazouz
Calcite | Level 5

thank you  @Kurt_Bremser

your solution also works

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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