Help using Base SAS procedures

If first statements Part II

Reply
Contributor
Posts: 57

If first statements Part II

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;

PROC Star
Posts: 7,363

Re: If first statements Part II

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?

Contributor
Posts: 57

Re: If first statements Part II

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
Valued Guide
Posts: 632

Re: If first statements Part II

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;

Super User
Posts: 5,082

Re: If first statements Part II

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.

Contributor
Posts: 57

Re: If first statements Part II

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.

Valued Guide
Posts: 632

Re: If first statements Part II

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;

Contributor
Posts: 57

Re: If first statements Part II

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

PROC Star
Posts: 7,363

Re: If first statements Part II

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;

Ask a Question
Discussion stats
  • 8 replies
  • 235 views
  • 6 likes
  • 4 in conversation