Help using Base SAS procedures

question on "Finding the First or Last Observation in a Group"

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

question on "Finding the First or Last Observation in a Group"

I follow the steps provided in the following website http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001349233.htm.

proc sort data=Gta2 Out=Gta3;
   by Agmtcd;
run;


data sub_Gta3;
set Gta3;
by Agmtcd;
run;


data sub_Gta3;
set Gta3;
FirstAgmtcd=first.Agmtcd;
LastAgmtcd=last.Agmtcd;
run;

However, SAS says the variables "first.Agmtcd" and "last.Agmtcd" are not initialized. The format of Agmtcd is $10. Does anybody know what's wrong with my code?

Thanks a lot!


Accepted Solutions
Solution
‎12-31-2011 11:33 AM
PROC Star
Posts: 7,364

question on "Finding the First or Last Observation in a Group"

You have 3 datasteps when you should only have 2.  The first. and last. variables are only available when a by statement is present within the SAME datastep.  Thus, try it with:

proc sort data=Gta2 Out=Gta3;
   by Agmtcd;
run;


data sub_Gta3;
  set Gta3;
  by Agmtcd;FirstAgmtcd=first.Agmtcd;

  LastAgmtcd=last.Agmtcd;
run;

View solution in original post


All Replies
Solution
‎12-31-2011 11:33 AM
PROC Star
Posts: 7,364

question on "Finding the First or Last Observation in a Group"

You have 3 datasteps when you should only have 2.  The first. and last. variables are only available when a by statement is present within the SAME datastep.  Thus, try it with:

proc sort data=Gta2 Out=Gta3;
   by Agmtcd;
run;


data sub_Gta3;
  set Gta3;
  by Agmtcd;FirstAgmtcd=first.Agmtcd;

  LastAgmtcd=last.Agmtcd;
run;

New Contributor
Posts: 2

question on "Finding the First or Last Observation in a Group"

It works. Thanks a lot!!!

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 502 views
  • 1 like
  • 2 in conversation