Hi,
I have some data below that i generated using the following code
data new2;
set new;
if id^=lag(id) or v1^=lag(v1) then time+1;
else time+0;
run;
What i am trying to do is have the time variable reset to "1" once the ID variable changes (observation 21 - time "5" ) and for the rest of this dataset where the ID changes. Up until Obs 20 the time variable is correct.
Thank you
SAS Output
20130610111823 | 195 | 0.827831 | 13.1 | 1 |
20130610111823 | 195 | 0.765397 | 13.1 | 1 |
20130610111823 | 195 | 0.755809 | 13.1 | 1 |
20130610111823 | 195 | 0.572855 | 13.1 | 1 |
20130610111823 | 195 | 0.734431 | 13.1 | 1 |
20130610111823 | 217 | 0.845952 | 12.7 | 2 |
20130610111823 | 217 | 0.79176 | 12.7 | 2 |
20130610111823 | 217 | 0.767398 | 12.7 | 2 |
20130610111823 | 217 | 0.745255 | 12.7 | 2 |
20130610111823 | 217 | 0.92218 | 12.7 | 2 |
20130610111823 | 223 | 0.747259 | 13.2 | 3 |
20130610111823 | 223 | 0.722813 | 13.2 | 3 |
20130610111823 | 223 | 0.733111 | 13.2 | 3 |
20130610111823 | 223 | 0.795692 | 13.2 | 3 |
20130610111823 | 223 | 0.863481 | 13.2 | 3 |
20130610111823 | 260 | 0.759839 | 12.6 | 4 |
20130610111823 | 260 | 0.72373 | 12.6 | 4 |
20130610111823 | 260 | 0.74912 | 12.6 | 4 |
20130610111823 | 260 | 0.71509 | 12.6 | 4 |
20130610111823 | 260 | 0.816424 | 12.6 | 4 |
20130612092329 | 254 | 0.593263 | 11.1 | 5 |
20130612092329 | 254 | 0.592495 | 11.1 | 5 |
20130612092329 | 257 | 0.578249 | 7.9 | 6 |
20130612092329 | 257 | 0.980644 | 7.9 | 6 |
20130612092329 | 258 | 0.818048 | 11 | 7 |
20130612092329 | 258 | 0.885162 | 11 | 7 |
20130612092329 | 258 | 0.793767 | 11 | 7 |
20130612092329 | 258 | 0.362111 | 11.7 | 7 |
20130612092329 | 258 | 0.539453 | 11.7 | 7 |
20130612092329 | 258 | 0.659541 | 11.9 | 7 |
Assuming the data is sorted by id the following code works.
data new; * read test data ;
input id $14. v1 v2 v3 old_time;
cards;
20130610111823 195 0.827831 13.1 1
20130610111823 195 0.765397 13.1 1
20130610111823 195 0.755809 13.1 1
20130610111823 195 0.572855 13.1 1
20130610111823 195 0.734431 13.1 1
20130610111823 217 0.845952 12.7 2
20130610111823 217 0.79176 12.7 2
20130610111823 217 0.767398 12.7 2
20130610111823 217 0.745255 12.7 2
20130610111823 217 0.92218 12.7 2
20130610111823 223 0.747259 13.2 3
20130610111823 223 0.722813 13.2 3
20130610111823 223 0.733111 13.2 3
20130610111823 223 0.795692 13.2 3
20130610111823 223 0.863481 13.2 3
20130610111823 260 0.759839 12.6 4
20130610111823 260 0.72373 12.6 4
20130610111823 260 0.74912 12.6 4
20130610111823 260 0.71509 12.6 4
20130610111823 260 0.816424 12.6 4
20130612092329 254 0.593263 11.1 5
20130612092329 254 0.592495 11.1 5
20130612092329 257 0.578249 7.9 6
20130612092329 257 0.980644 7.9 6
20130612092329 258 0.818048 11 7
20130612092329 258 0.885162 11 7
20130612092329 258 0.793767 11 7
20130612092329 258 0.362111 11.7 7
20130612092329 258 0.539453 11.7 7
20130612092329 258 0.659541 11.9 7
;;;
run
;;;
data new2; * create new data ;
set new;
by id;
if first.id then time=0;
if id^=lag(id) or v1^=lag(v1) then time+1;
run;
Hi, thank you for the reply.
The ID variable is the first variable on the far left.
20130610111823 is the first ID up until the 20th observation. 21 is the new ID
Why don't you just
SET...
BY ID TIME NOSTORTED;
The you can use first and last variables.
The time variable is the one that I am creating, thats where my issue is. I am trying to get it to begin again at time 1 for each unique ID.
OK then V1.
Assuming the data is sorted by id the following code works.
data new; * read test data ;
input id $14. v1 v2 v3 old_time;
cards;
20130610111823 195 0.827831 13.1 1
20130610111823 195 0.765397 13.1 1
20130610111823 195 0.755809 13.1 1
20130610111823 195 0.572855 13.1 1
20130610111823 195 0.734431 13.1 1
20130610111823 217 0.845952 12.7 2
20130610111823 217 0.79176 12.7 2
20130610111823 217 0.767398 12.7 2
20130610111823 217 0.745255 12.7 2
20130610111823 217 0.92218 12.7 2
20130610111823 223 0.747259 13.2 3
20130610111823 223 0.722813 13.2 3
20130610111823 223 0.733111 13.2 3
20130610111823 223 0.795692 13.2 3
20130610111823 223 0.863481 13.2 3
20130610111823 260 0.759839 12.6 4
20130610111823 260 0.72373 12.6 4
20130610111823 260 0.74912 12.6 4
20130610111823 260 0.71509 12.6 4
20130610111823 260 0.816424 12.6 4
20130612092329 254 0.593263 11.1 5
20130612092329 254 0.592495 11.1 5
20130612092329 257 0.578249 7.9 6
20130612092329 257 0.980644 7.9 6
20130612092329 258 0.818048 11 7
20130612092329 258 0.885162 11 7
20130612092329 258 0.793767 11 7
20130612092329 258 0.362111 11.7 7
20130612092329 258 0.539453 11.7 7
20130612092329 258 0.659541 11.9 7
;;;
run
;;;
data new2; * create new data ;
set new;
by id;
if first.id then time=0;
if id^=lag(id) or v1^=lag(v1) then time+1;
run;
If the IDs aren't sorted, but are grouped together this would also work.
data new2;
set new;
if id^=lag(id) then time=0;
if v1^=lag(v1) then time+1;
run;
Thank you! this works.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.