DATA Step, Macro, Functions and more

Subtraction

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Subtraction

Hello,
I would like to calculate the subtraction of n and n-1

 

data table;

input n;

cards;

15

24

8

9

2

;

run;

 

and get in a table

N           S

15          .

24          9

8           -16

9             1

2             -7

 

 

Thanks for your help

 

 


Accepted Solutions
Solution
‎01-31-2018 09:03 AM
Super Contributor
Posts: 338

Re: Subtraction

Just add:

s = n - lag(n);

//Fredrik

View solution in original post


All Replies
Super User
Super User
Posts: 9,193

Re: Subtraction

data table;
input n;
cards;
15
24
8
9
2
;
run;

data want;
set table;
if _n_ ne 1 then s=n - lag(n);
run;
Solution
‎01-31-2018 09:03 AM
Super Contributor
Posts: 338

Re: Subtraction

Just add:

s = n - lag(n);

//Fredrik

Super User
Posts: 10,610

Re: Subtraction


data table;
  input n;
cards;
15
24
8
9
2
;
run;

data want;
 set table;
 s=dif(n);
run;
Contributor
Posts: 37

Re: Subtraction

data want;
set table;
want=n-lag(n);
run;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 119 views
  • 3 likes
  • 5 in conversation