turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to retain a value from the previous row to do ...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-26-2017 05:46 PM

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!

Accepted Solutions

Solution

06-27-2017
10:28 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-26-2017 06:00 PM

Here is one way:

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

Art, CEO, AnalystFinder.com

All Replies

Solution

06-27-2017
10:28 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-26-2017 06:00 PM

Here is one way:

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

Art, CEO, AnalystFinder.com

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 10:29 AM

Thank you!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 05:36 PM

Hello,

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 06:04 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-26-2017 08:43 PM

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

Jag

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 03:53 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 04:25 AM

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

Jag

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 01:09 PM - edited 06-27-2017 01:21 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-27-2017 10:29 AM

Thank you!