BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Golf
Pyrite | Level 9

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
Golf
Pyrite | Level 9
Thank you for your suggestion.
Best,

View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

In a data step:

 

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

PG
Golf
Pyrite | Level 9
Thank You.
Golf
Pyrite | Level 9

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

mkeintz
PROC Star

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

--------------------------
Golf
Pyrite | Level 9
Thank you for your suggestion.
Best,
FreelanceReinh
Jade | Level 19

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 2109 views
  • 4 likes
  • 5 in conversation