Help using Base SAS procedures

How to calculate the difference between two observation on different rows

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to calculate the difference between two observation on different rows

 Hi,

 

I have a table that shows an ID and two numeric variables, as in the example below:

IDVAR1VAR2
ID12010
ID22111
ID32212

 

I have to add in this table an other column (e.g. TOT) that calculate the difference between var1 to the first row and var2 to the second and so on, as in the example below:

IDVAR1VAR2TOT
ID120109
ID221119
ID3221222

 

Could anyone help me?

Thanks for your assistance.


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 84

Re: How to calculate the difference between two observation on different rows

data want;
  merge 
    have 
    have(keep=var2 rename=(var2=next_var2) firstobs=2) 
      end=done;
  if not done then
    tot=var1-next_var2;
  drop next_var2;
run;
  

View solution in original post


All Replies
Solution
2 weeks ago
PROC Star
Posts: 84

Re: How to calculate the difference between two observation on different rows

data want;
  merge 
    have 
    have(keep=var2 rename=(var2=next_var2) firstobs=2) 
      end=done;
  if not done then
    tot=var1-next_var2;
  drop next_var2;
run;
  
New Contributor
Posts: 3

Re: How to calculate the difference between two observation on different rows

Thank you so much!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 120 views
  • 0 likes
  • 2 in conversation