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.
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.
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
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.
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.
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.