DATA Step, Macro, Functions and more

Tip for sas code

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Tip for sas code

I have a dataset with the following columns: dates, client name, jail record, jail facility name

I need to query the data in base sas as follow:

If a client has a jail record and there is a previous jail record within 30 days where the jail facility was different then i want to code it as an event for each client.

Any ideas how to do that please!


Accepted Solutions
Solution
‎01-29-2017 05:45 AM
Super User
Posts: 5,079

Re: Tip for sas code

Here's a reasonable approach:

 

proc sort data=have;

by client date;

run;

 

data want;

set have;

by client;

days_lapsed = dif(date);

prior_facility = lag(facility);

if first.client=0 and days_lapsed < 30 and facility = prior_facility then event='Y';

drop days_lapsed prior_facility;

run;

 

This assumes that your date variable is actually a SAS date, not a character string.

View solution in original post


All Replies
Solution
‎01-29-2017 05:45 AM
Super User
Posts: 5,079

Re: Tip for sas code

Here's a reasonable approach:

 

proc sort data=have;

by client date;

run;

 

data want;

set have;

by client;

days_lapsed = dif(date);

prior_facility = lag(facility);

if first.client=0 and days_lapsed < 30 and facility = prior_facility then event='Y';

drop days_lapsed prior_facility;

run;

 

This assumes that your date variable is actually a SAS date, not a character string.

Occasional Contributor
Posts: 19

Re: Tip for sas code

Thanks - I will use this approach.
Super User
Posts: 6,928

Re: Tip for sas code

Sort by client name and date. Then you can use the lag() function to compare variables with values from the preceding observation.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,483

Re: Tip for sas code

Do you have some id such as SSN? Note that people involved in jail often have a good reason to hide their real identity and multiple arrests and/or convictions and a simple name match is likely to be insufficient. Also is Rob Smith the same as Robert Smith or Bob Smith?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 145 views
  • 1 like
  • 4 in conversation