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

Hello SAS world,

 

I need your help with the following: 

Have:

PatientID            Date 

X                      01/22/2012

X                      01/30/2012

X                      01/31/2012

X                      02/02/2012

 

Want:

PatientID            Date                      Diff

X                      01/22/2012                0

X                      01/30/2012                8

X                      01/31/2012                1

X                      02/02/2012                2

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  by PatientID;
  diff=ifn(first.PatientID eq 0,dif(date),0);
run;

Art, CEO, AnalystFinder.com

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  by PatientID;
  diff=ifn(first.PatientID eq 0,dif(date),0);
run;

Art, CEO, AnalystFinder.com

Sujithpeta
Quartz | Level 8

Hello,

 

 Untitled.png

 

Thanks for the earlier help, I need your help to solve the next problem.

 

From the previous output I got the difference between dates. In the study period, I want to flag patients where there is difference of >120 days and mark all the dates after that >120 days. If there is a patients who have 2 rows of >120 days then I want to mark first >120 days to the 2nd >120 days as "R1" and "R2" for dates greater than 2nd >120 days.

 

Thanks!

art297
Opal | Level 21

You will get more responses if you post your new question as a new question .. not a continuation of the previous question.

 

Art, CEO, AnalystFinder.com

 

Jagadishkatam
Amethyst | Level 16

another way

 

option missing=0;
data have;
input PatientID $          Date :mmddyy10.;
lagdate=intck('day',lag(date),date);
format date date9.;
cards;
X                      01/22/2012
X                      01/30/2012
X                      01/31/2012
X                      02/02/2012
;
Thanks,
Jag
ErikLund_Jensen
Rhodochrosite | Level 12

@Jagadishkatam

 

You will need a line to reset the calculated difference to zero for the first obs in an ID group. Something like this:

 

 option missing=0;
data have;
input PatientID $ Date :mmddyy10.;
lagdate=intck('day',lag(date),date);
if PatientID ne lag(PatientID) then lagdate = 0;
format date date9.;
cards;
X 01/22/2012
X 01/30/2012
X 01/31/2012
X 02/02/2012
y 01/22/2017
y 01/30/2017
y 01/31/2017
y 02/02/2017
;
run;

 

Jagadishkatam
Amethyst | Level 16
I don't need the step of lagdate = 0; because in my code i used options missing=0; what this does is it assigns the value 0 to the missing values.
Thanks,
Jag
MikeZdeb
Rhodochrosite | Level 12

Hi ... OPTIONS MISSING=0 does NOT change values of missing data in a data set to ZERO. It only changes the way they appear when printed.

 

1169
1170 options missing=0;
1171 data x;
1172 x = 1;
1173 y = .;
1174 z = x + y;
1175 run;

NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line) : (Column).
1 at 1174:7

 

If you print data set X you see ZEROES (due to the OPTIONS statement), but if you look at the LOG, you'll notice that SAS still sees MISSING data.

 

From online help (notice that it says printed)...

 

Syntax
MISSING=<'>character<'>

Syntax Description
<'>character<'>
specifies the value to be printed. The value can be any character. Single or double quotation marks are optional. The period is the default.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 9 replies
  • 9559 views
  • 3 likes
  • 5 in conversation