DATA Step, Macro, Functions and more

retain first of a type and first of another type

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

retain first of a type and first of another type

Hi,

I got a large numbers of events which is a sort of interval where I want to count days from a start event point (date)  to an end point(another date). I need the first start1 og end1/end2 in every interval. Do you have any smart suggestion to do that? after the specific observation has been output i will count the days by retain. Thanks in advance.

 

idx start1 ----->

idx start1

idx start1

idx end1------->

idx start1 ----->

idx start1

idx start1

idx end1------->

idy start1 ----->

idy start1

idy start1

idy end2------->


Accepted Solutions
Solution
‎03-17-2017 05:35 PM
Super User
Posts: 5,085

Re: retain first of a type and first of another type

You'll probably see a few somewhat similar solutions.  Here's the variation I would use:

 

data want;

set have;

by caseid type notsorted;

if (first.type and type='typex') or (last.type and type='typey');

run;

 

You have to match whatever is in your data (is it "typey" or "type y" for example).

View solution in original post


All Replies
Super User
Posts: 17,864

Re: retain first of a type and first of another type

Is that your sample data? Please show what you want as output and make sure your sample data is reflective of your actual data. 

Super User
Super User
Posts: 7,407

Re: retain first of a type and first of another type

Show test data  in the form of a datastep and what what you want the output to look like.  We can't tell anything from what you have posted.

Contributor
Posts: 55

Re: retain first of a type and first of another type

The data is case date events and I want the first typex as startpoint date and always typey as end date. I think I need a counter which os reset by the endtype??? any suggestions are welcome.

 

my data looks like this

caseid, date, type

122220 12/7/2016 typex ----->want this one in output, first typex.sorted by date and caseid

122220 7/8/2016 typex

122220 7/9/2016 type x

122220 10/10/2016 typey------->want this one in output, type y

122220 17/10/2016 typex ----->want this one in output, first typex, sorted by date and caseid

122220 24/10/2016 typex

122220 7/9/2016 typex

122220 10/10/2016 typey------->want this one in output, type y

122300 3/3/2016 typex ----->want this one in output, first typex sorted by date and caseid

122300 10/3/2016 typex

122300 7/9/2016 typex

122300 10/10/2016 type y------->want this one in output, type y

122300 17/10/2016 typex ----->want this one in output, first typex, sorted by date and caseid

122300 24/10/2016 typex

122300 26/10/2016 type x

122300 26/11/2016 type y------->want this one in output, type y

Solution
‎03-17-2017 05:35 PM
Super User
Posts: 5,085

Re: retain first of a type and first of another type

You'll probably see a few somewhat similar solutions.  Here's the variation I would use:

 

data want;

set have;

by caseid type notsorted;

if (first.type and type='typex') or (last.type and type='typey');

run;

 

You have to match whatever is in your data (is it "typey" or "type y" for example).

Contributor
Posts: 55

Re: retain first of a type and first of another type

great simple solutionwhich works as wanted - thanks

 

☑ This topic is SOLVED.

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

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