DATA Step, Macro, Functions and more

SAS beginner question - How to get only the first row of data the satisfies a certain condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

SAS beginner question - How to get only the first row of data the satisfies a certain condition

Hi everyone,

I am a beginner to SAS programming and would like some advice on a specific issue. I have my data laid out like this:

Customer ID   date                   Code            
1                   1/1/2013             HPP

2                   1/2/2013             ABC

3                   1/5/2013             ABC

4                   1/20/2013           HPP

I would like to only get the customer ID which has the first occurrence of Code "ABC", which is row 2. How do I go about doing this?

Thanks in advance.


Accepted Solutions
Solution
‎11-21-2013 11:13 PM
Super User
Posts: 5,082

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

Here's one approach:

data want;

  set have (obs=1);

  where code='ABC';

run;

In older releases of the software, the combination of WHERE and OBS was illegal.  After a while, SAS decided on what the right way to interpret that combination should be, and made it do what you are asking for.

Good luck.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,128

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

Please try the below code, this will produce the output which has the first occurrence of code

data have;

    input Customer_ID   date : mmddyy10.   Code$;

    format date mmddyy10.;

cards;

1                   1/1/2013             HPP

2                   1/2/2013             ABC

3                   1/5/2013             ABC

4                   1/20/2013           HPP

;

proc sort data=have;

    by Code;

run;

data want;

    set have;

    by Code;

    if first.Code;

run;

Thanks,

Jagadish

Thanks,
Jag
Occasional Contributor
Posts: 5

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

Hi Jagadishkatam,

Thanks for your prompt response. But what if for this specific purpose I don't want to sort the data by code? I just want to find the first occurrence where Code = "ABC" on the unsorted data.

Thanks.

Solution
‎11-21-2013 11:13 PM
Super User
Posts: 5,082

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

Here's one approach:

data want;

  set have (obs=1);

  where code='ABC';

run;

In older releases of the software, the combination of WHERE and OBS was illegal.  After a while, SAS decided on what the right way to interpret that combination should be, and made it do what you are asking for.

Good luck.

Occasional Contributor
Posts: 5

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

Thanks so much to you both Astounding and Jagadishkatam. I just have one more general question.

I need to translate some code from Visual FoxPro to SAS. Are there any functions in SAS like seek and found, set order to, and inlist like in VFP? Is SAS capable of managing large databases like VFP? Just to give some context I need to run and manipulate the data from 2 datasets each having 450000 rows.

Sorry if the question don't make sense, gradually learning here Smiley Happy

Super User
Posts: 10,500

Re: SAS beginner question - How to get only the first row of data the satisfies a certain condition

You may need to explain to us what the VFP functions do. Though it would be better to start a new question as this one has been marked answered an may not get many responses. Also it is a new topic.

Since we have had questions about determining how many records were in SAS data sets when the number exceeded the 12 significant digits, I think you can say that SAS will handle data sets of that size.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 280 views
  • 3 likes
  • 4 in conversation