BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aminkarimid
Lapis Lazuli | Level 10

Hello everybody;

I want to calculate a division which the nominator and denominator are in different rows. Please consider the table below:

table 1:

namedatetimeIntradaydailysumadjusted
A12/1/20139:30:001   
A12/1/201310:00:0023  
A12/2/201310:30:003   
A12/2/201311:00:0025  
A12/3/201310:00:002   
A12/3/201310:30:001   
A12/3/201311:00:0014  
A12/4/201311:30:001   
A12/4/201312:00:003   
A12/4/201312:30:0037  
A12/7/20139:30:001   
A12/7/201310:00:001221 
A12/8/201310:30:002  0.095238
A12/8/201311:00:0035230.142857
A12/17/201311:30:001  0.043478
A12/17/201312:00:0012230.043478
A12/18/201312:30:0022200.086957
A12/28/20139:30:001  0.05
A12/28/201310:00:0012180.05
B12/8/201310:30:002   
B12/8/201311:30:002   
B12/8/201312:30:0015  
B12/14/20139:30:001   
B12/14/201310:30:0012  
B12/15/201312:00:002   
B12/15/201312:30:0013  
B12/24/20139:30:0011  
B12/25/201310:00:002   
B12/25/201310:30:002415 
B12/26/201312:00:0022120.133333
B12/30/20139:30:003  0.25
B12/30/201310:00:002  0.166667
B12/30/201310:30:0016160.083333
B1/3/201411:00:001  0.0625
B1/3/201411:30:001  0.0625
B1/13/201412:00:0013160.0625

 

I want to create the adjusted variable which is shown in the table 1. This variable is calculated using the formula which is described below:

The intraday variable is divided by the sum variable of previous date. For instance, values '0.095238' and '0.142857'
at '12/8/2013' are 2/21 and 3/21 respectively.

 

How can I do that?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

data want (drop=last_sum have last_not_missing_sum);
  set table1;
  retain have last_not_missing_sum;
  by name;
  last_sum=lag(sum);
  if first.name then do;
    have=0;
    call missing(last_sum);
    call missing(last_not_missing_sum);
  end;
  if not missing(last_sum) then do;
    have=1;
    last_not_missing_sum=last_sum;
  end;
  if have then adjusted=Intraday/last_not_missing_sum;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

1 REPLY 1
art297
Opal | Level 21

Here is one way:

data want (drop=last_sum have last_not_missing_sum);
  set table1;
  retain have last_not_missing_sum;
  by name;
  last_sum=lag(sum);
  if first.name then do;
    have=0;
    call missing(last_sum);
    call missing(last_not_missing_sum);
  end;
  if not missing(last_sum) then do;
    have=1;
    last_not_missing_sum=last_sum;
  end;
  if have then adjusted=Intraday/last_not_missing_sum;
run;

Art, CEO, AnalystFinder.com

 

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 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
  • 1 reply
  • 416 views
  • 0 likes
  • 2 in conversation