Hi there,
Please see below is what I try to get:
| ID | visit | want | |
| 01 | 30 | 0 | |
| 01 | 34 | 4 | = 34-30 |
| 01 | 39 | 5 | = 39-34 |
| 01 | 45 | 6 | = 45-39 |
| 02 | 35 | 0 | = 35-35 |
| 02 | 40 | 5 | = 40-35 |
| 02 | 42 | 2 | = 42-40 |
How to code in SAS to get the value in "want"?
any help will be very appriciate.
Thanks in advance!
data have;
input ID visit;
cards;
01 30
01 34
01 39
01 45
02 35
02 40
02 42
;
data want;
set have;
by id visit;
want=visit-lag(visit);
if first.id then want=0;
run;
data have;
input ID visit;
cards;
01 30
01 34
01 39
01 45
02 35
02 40
02 42
;
data want;
set have;
by id visit;
want=visit-lag(visit);
if first.id then want=0;
run;
Minor change suggestion to @Jagadishkatam
data want; set have; by id; want = dif(visit); if first.id then want=0; run;
The Dif function is the equivalent of : var- lag(var);
Thanks so much!!
it's very helpful!
Thank you so much for your prompt solution response.
I appreciated it.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.