BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yp2609
Fluorite | Level 6

yp2609_0-1686147884665.png

I want to divide 2nd row of j_top by the 1st row of j_denom; divide 3rd row of j_top by 2nd rows of j_denom; so on and so forth. 

lag function is not working as I want it to be. 

Can anyone provide code for this? Thank you so much!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

divide 2nd row of j_top by the 1st row of j_denom;

data want;
  set have;
  lag_j_denom=lag(j_denom);
  if _n_ =1 then want = j_top;
 else want=j_top/lag_j_denom;
run;

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Show us your code. Show us the log. Explain what is wrong.

--
Paige Miller
yp2609
Fluorite | Level 6

Nothing is wrong. I just have no clue how to perform this code. 

ballardw
Super User

@yp2609 wrote:

 

I want to divide 2nd row of j_top by the 1st row of j_denom; divide 3rd row of j_top by 2nd rows of j_denom; so on and so forth. 

lag function is not working as I want it to be. 

Can anyone provide code for this? Thank you so much!

 


When you say code isn't working you should show the code and possibly the LOG if you are getting unexpected notes in the log.

 

Because Lag, and its partner Dif, function is a queue function then using it conditionally is the main cause of unexpected results as the "lag" then becomes the last time the condition was true.

 

 

Tom
Super User Tom
Super User

divide 2nd row of j_top by the 1st row of j_denom;

data want;
  set have;
  lag_j_denom=lag(j_denom);
  if _n_ =1 then want = j_top;
 else want=j_top/lag_j_denom;
run;

 

yp2609
Fluorite | Level 6
Thank you so much!
sbxkoenk
SAS Super FREQ

Hello @yp2609 ,

 

Your topic title (title of your post) is misleading.

But if you don't know how a data step works, I agree you have difficulties in knowing whether you need lag or lead.

 

I hope it's clear by now you only need lag.

When processing observation k you need info that is from observation (k-1).

That's looking back.

Looking ahead is not needed here.

 

BR,

Koen

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 430 views
  • 1 like
  • 5 in conversation