BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shukuchi
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

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
shukuchi
Fluorite | Level 6

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.

Astounding
PROC Star

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.

shukuchi
Fluorite | Level 6

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

ballardw
Super User

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 20374 views
  • 5 likes
  • 4 in conversation