🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Quartz | Level 8

## first difference data (selection)

Hello,

I have the following data set.

city     Year     D87      X     dx

1        82      0           a       -

2        87      1          b       (b-a)

3        82      0          c       -

4        87      1          d      (d-c)

5        82      0          e       -

6        87      1          f       (f-e)

What procedure should be used to achieve dx ?

Thank You

1 ACCEPTED SOLUTION

Accepted Solutions
Quartz | Level 8

## Re: first difference data (selection)

Thank you for your suggestion.
Best,
7 REPLIES 7
Opal | Level 21

## Re: first difference data (selection)

In a data step:

pX = lag(X);
if D87 then dx = X - pX;
drop pX;

PG
Quartz | Level 8

Thank You.
Quartz | Level 8

## Re: first difference data (selection)

L_x = lag(x);
dx = x-L_x;
if d87 = 0 then dx = .;

Jade | Level 19

## Re: first difference data (selection)

Or you could use the DIF function, defined as X-lag(X):

dx=dif(x);

if d87=0 then dx=.;

Or slightly more economical.

dx= ifn(d87=1,dif(x),.) ;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Quartz | Level 8

## Re: first difference data (selection)

Thank you for your suggestion.
Best,
Jade | Level 19

## Re: first difference data (selection)

Hello @Golf,

Glad to see that mkeintz's solution worked for you. Then it would be fair and help later readers if you marked his helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select his post as the solution after clicking "Not the Solution" in the option menu (see icon below) of the current solution.

Super User

## Re: first difference data (selection)

Using the IFN() and DIF() functions, as suggested by @mkeintz, is the most economical way.

Discussion stats
• 7 replies
• 615 views
• 4 likes
• 5 in conversation