DATA Step, Macro, Functions and more

How to select first obs of multiple records

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

How to select first obs of multiple records

Hello 

 

I have a variable called Plural in my dataset (numeric; 1-8; 9=unknown). Since for those w. Plural >1 will have duplicate values for the variables I am interested in, I want  SAS to keep only one record (doesn't matter which) when Plural >1. Does anyone know how to do this? 

 

Thank you! 

 

 


Accepted Solutions
Solution
‎08-03-2017 12:38 PM
Trusted Advisor
Posts: 1,580

Re: How to select first obs of multiple records

Check next code:

proc sort data=have out=want nodupkey;
  by DOB DATE1 PLURAL;
run;

Do you prefer the record with FEEDIN=YES or FEEDING=NO ?

If you have a preference then use next code:

proc sort data=have out=temp;
  by DOB DATE1 PLURAL 
       {ascending or descending } FEEDING;
run;

data want;
 set temp;
  by DOB DATE1 PLURAL;
       if first.plural;
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,580

Re: How to select first obs of multiple records

You haven't post test data.

You can use proc sort with option NODUPKEY to achieve one observation of

any plural occurence.

Occasional Contributor
Posts: 17

Re: How to select first obs of multiple records

Thank you for your reply. But if I PROC SORT w. NODUPKEY by Plural SAS will get rid of the most obs because most of the obs in my dataset has Plural=1. I want SAS to pick one record only when Plural >1. 

Trusted Advisor
Posts: 1,580

Re: How to select first obs of multiple records

please post an example - some test data to explain what you have and the desired result.

Occasional Contributor
Posts: 17

Re: How to select first obs of multiple records

ID       DOB        DATE1     DURATION    PLURAL   FEEDING 

1        2/2/85       4/8/16           30                  2               YES

2        2/2/85       4/8/16           30                  2                NO

3        4/2/79       5/1/14            1                   3             

4        4/2/79       5/1/14            1                   3                NO

5        4/2/79       5/1/14            1                   3

6        8/1/12       5/1/16           10                  1                 NO

7        1/2/14      10/1/17           2                   1                 

8        8/1/12      8/1/15             1                   1                 NO 

 

So here's what my dataset kind of looks like. 

ID and Feeding are collected from child. DOB, DATE1, DURATION, PLURAL are from mother. I am interested in the variable DURATION. Since DURATION is assigned to mother I'd like to keep only one record of this variable when Plural is greater than 1. If Plural=1 then there is only one record so I don't have to worry about it. 

 

Hope this makes sense! Thank you!

 

Solution
‎08-03-2017 12:38 PM
Trusted Advisor
Posts: 1,580

Re: How to select first obs of multiple records

Check next code:

proc sort data=have out=want nodupkey;
  by DOB DATE1 PLURAL;
run;

Do you prefer the record with FEEDIN=YES or FEEDING=NO ?

If you have a preference then use next code:

proc sort data=have out=temp;
  by DOB DATE1 PLURAL 
       {ascending or descending } FEEDING;
run;

data want;
 set temp;
  by DOB DATE1 PLURAL;
       if first.plural;
run;
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 97 views
  • 0 likes
  • 2 in conversation