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 🙂 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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