BookmarkSubscribeRSS Feed
SannaSanna
Quartz | Level 8

Thank you to Reeza and Arthur for their help and explanation to my previous question.  I want to ask a follow-up.  What if I was to count the first observations for three different variables?  I want outputted the first stukey and first school and first fcode.  Could I do this?   Would I always select the first dot of the last variable regardless of how many variables I select?  Thank you!

data test;

     set enr;

     by stukey school fcode;

     if first.fcode; run;

8 REPLIES 8
art297
Opal | Level 21

Not sure what you are asking.  Can you provide an example that shows what test looks like (i.e., with some example data preferably in the form of a datastep), as well as another table that shows the result of what you are trying to accomplish?

SannaSanna
Quartz | Level 8

I have a dataset with stukeys schools and fcodes.  I am interested in finding every different fcode for each stukey assigned by every school.    Okay here is the sample data:

Notice that in the text file, the very last record is a duplicate and I would expect the output to exclude that.

data test;

input stukey $ school $ fcode $;

cards;

992346 20311 D

992346 23659 C

992346 20311 B

992346 55236 A

992346 20311 D

;

Here is my expected results:

99234620311D
99234623659C
99234620311B
99234655236A
ArtC
Rhodochrosite | Level 12

Does this do what you want?

data test;
input stukey $ school $ fcode $;
cards;
992346 20311 D
992346 23659 C
992346 20311 B
992346 55236 A
992346 20311 D
run;

proc sort data=test out=test2;
   by stukey school fcode;
   run;
data enr dup;
   set test2;
   by stukey school fcode;
   if first.fcode then output enr;
   else output dup;
   run;

Astounding
PROC Star

If you want just one observation per combination of the three variables, does it matter if you take the first one, the last one, or a middle one?  If not, try:

proc sort data=test out=test2 NODUPKEY;

   by stukey school fcode;

run;

If you have to take the first one (and not the last or a middle one), try:

proc sort data=test;

   by stukey school fcode;

run;

data just_first;

   set test;

   by stukey school fcode;

   if first.fcode;

run;

But really, you should take ArtC's suggestion from earlier, and try to understand how these tools work.  After sorting, try:

data temp;

   set test;

   by stukey school fcode;

   first_stukey = first.stukey;

   last_stukey = last.stukey;

   first_school = first.school;

   last_school = last.school;

   first_fcode = first.fcode;

   last_fcode = last.fcode;

run;

proc print data=temp (obs=50);

run;

These will be important tools to know about in the long run.

Good luck.

SannaSanna
Quartz | Level 8

I was able to get it to work.  Thank you so much for your time and I will definitely look further into ArtC's approach.

ArtC
Rhodochrosite | Level 12

Because FCODE is nested within SCHOOL within STUKEY, the groups are not necessarily 'FIRST' at the same time.  If FIRST.STUKEY is true, the other three must also be true.  But the reverse is not true.

Examine the output from the following:

proc sort data=sashelp.class out=class;
by sex age;
run;

data new(keep=sex age fsex fage);
set class;
by sex age;
fsex=first.sex;
fage=first.age;
run;

proc print data=new;
run;

SannaSanna
Quartz | Level 8

It worked.  Thank you so much for your effort and time.

art297
Opal | Level 21

There are a couple of ways to accomplish what you want.  Here is one:

data test;

input stukey $ school $ fcode $;

cards;

992346 20311 D

992346 23659 C

992346 20311 B

992346 55236 A

992346 20311 D

;

/* if you want to keep the current order of the records

you will have to add a variable showing the order.  e.g.:*/

data test;

  set test;

  recnum=_n_;

run;

/*then you have to sort the file*/

proc sort data=test nodupkey;

  by stukey school fcode;

run;

/*then sort the file back to the original order*/

proc sort data=test;

  by recnum;

run;

/*then get rid of the extra field*/

data test (drop=recnum);

  set test;

run;

proc print data=test;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 891 views
  • 6 likes
  • 4 in conversation