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

This is my sample dataset:

 

PtID visit Flag

001   1      0

001   2       .

001   3       .

002   1      1

002   2       . 

 

For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:

 

PtID visit Flag

001   1      0

001   2      1

001   3       .

002   1      1

002   2       . 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

@Abishekaa wrote:

This is my sample dataset:

 

PtID visit Flag

001   1      0

001   2       .

001   3       .

002   1      1

002   2       . 

 

For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:

 

PtID visit Flag

001   1      0

001   2      1

001   3       .

002   1      1

002   2       . 

 


Do you really mean "first.flag=0", or (as I suspect) you mean the first obs for a given ID has flag=0.   If it's the latter, then:

 

data want;
  set have;
  by ptid;
  if first.ptid=0 and lag(first.ptid)=1 and lag(flag)=0 then flag=0;
run;

The if statement tests for

  1. The current obs is the second for a given ID -- first.ptid=0 and lag(first.ptid)=1.  (or you could replace those 2 conditions with    dif(first.ptid)=-1  (dif(x) is defined as x-lag(x).
  2. The immediately preceding flag is zero  --  lag(flag)=0

 

 

 

--------------------------
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

--------------------------

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Are the visits always so nicely numbered (1,2,3,....)?

data want;
  set have ;
  if visit=2 and lag(flag)=0 then flag=1;
run;

Or not?

data want;
  set have ;
  by ptid ;
  if first.ptid then lag_flag=flag;
  else do;
    if lag_flag=0 then flag=1;
    lag_flag=.;
  end;
  retain lag_flag ;
  drop lag_flag;
run;
mkeintz
PROC Star

@Abishekaa wrote:

This is my sample dataset:

 

PtID visit Flag

001   1      0

001   2       .

001   3       .

002   1      1

002   2       . 

 

For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:

 

PtID visit Flag

001   1      0

001   2      1

001   3       .

002   1      1

002   2       . 

 


Do you really mean "first.flag=0", or (as I suspect) you mean the first obs for a given ID has flag=0.   If it's the latter, then:

 

data want;
  set have;
  by ptid;
  if first.ptid=0 and lag(first.ptid)=1 and lag(flag)=0 then flag=0;
run;

The if statement tests for

  1. The current obs is the second for a given ID -- first.ptid=0 and lag(first.ptid)=1.  (or you could replace those 2 conditions with    dif(first.ptid)=-1  (dif(x) is defined as x-lag(x).
  2. The immediately preceding flag is zero  --  lag(flag)=0

 

 

 

--------------------------
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

--------------------------
Abishekaa
Obsidian | Level 7

This works perfectly! Thank you so much.  I didn't realize the lag function could be used to fully solve this problem 🙂 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1338 views
  • 1 like
  • 3 in conversation