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-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1200 views
  • 1 like
  • 5 in conversation