BookmarkSubscribeRSS Feed
akrishkumar
Calcite | Level 5

How do i find second place for he below

 

Thanks

 

data order2;
set order;
by agegroup;
if first.agegroup then
do;
group_place='First Place';*the top finisher of each agegroup;
end;
else if last.agegroup then
do;
group_place='Last Place';
end;
if first.agegroup or last.agegroup;*or if first.agegroup or last.agegroup then output order2 or if first.agegroup or last.agegroup then output;
run;

6 REPLIES 6
ballardw
Super User

Please provide some example data and what you expect for output.

Define the conditions of what defines "second place".

 

If your data is sorted such that some other variable controls what order records are within the agegroup you can get "order" position with something like this:

data order2;
   set order;
   by agegroup;
   retain order;
   if first.agegroup then
   do;
      group_place='First Place';*the top finisher of each agegroup;
      order=1;
   end;
   else order+1;
   if last.agegroup and not(first.agegroup) then
      group_place='Last Place';
run;

 

Currently your last line of code:
if first.agegroup or last.agegroup;

would prevent output of anything resembling "second place".

akrishkumar
Calcite | Level 5
Your hometown is having a walk around the town square to raise money for
the library.
You have the following data: entry number, age group, and finishing time.
(Notice that there is more than one observation per line of data.)

entry agegroup times
54 youth 35.5
21 adult 21.6
6 adult 25.8
13 senior 29.0
38 senior 40.3
19 youth 39.6
3 adult 19.0
25 youth 47.3
create a new variable for overall finishing place(ranking) and print the
results.
print the first (top finisher)and last finisher of each agegroup
in privious example create a new varible and assign the rank of each
walker in each agegroup and then choose just the first two top finnisher in
each agegroup
andreas_lds
Jade | Level 19

@akrishkumar wrote:
Your hometown is having a walk around the town square to raise money for
the library.
You have the following data: entry number, age group, and finishing time.
(Notice that there is more than one observation per line of data.)

entry agegroup times
54 youth 35.5
21 adult 21.6
6 adult 25.8
13 senior 29.0
38 senior 40.3
19 youth 39.6
3 adult 19.0
25 youth 47.3
(1) create a new variable for overall finishing place(ranking) and print the
results.
(2) print the first (top finisher)and last finisher of each agegroup
(3) in privious example create a new varible and assign the rank of each
walker in each agegroup and then choose just the first two top finnisher in
each agegroup

Please post the data in usable form! I don't see multiple observations in the data.

So you have three task, please show the code you have for the first two. Use the "insert code button", but before you do this: format the code.

qoit
Pyrite | Level 9

Hi, since the BY grouping is done by a single variable, did you mean find the second observation?

akrishkumar
Calcite | Level 5
Yes please I have the first position and last for walkers...I want the
second position in the race.

Thanks
qoit
Pyrite | Level 9

Hi, does the below help, I had to create a dummy dataset to ensure it works for you...

 

data have;
	infile datalines truncover;
	length name $5. agegrp $18.;
	input name:$  agegrp & $ finishtime:time.;
	format finishtime time.;
	datalines;
john  between 15 and 30  00:01:12
jake  under 15           00:01:14
jason under 15           00:01:50
amy   between 15 and 30  00:00:59
jamie between 15 and 30  00:00:48
scott under 15           00:00:58
Cam   under 15           00:02:01
jacob under 15           00:01:45
jacob between 15 and 30  00:01:41
;
run;

proc sort data=have;
	by agegrp finishtime;
run;

data want;
	set have;
	by agegrp finishtime;
	retain freq;
	format place $20.;

	if first.agegrp then
		do;
			place = 'First Place';
			freq = 0;
		end;

	freq + 1;

	if freq = 2 then
		place = 'Second Place';

	if last.agegrp then
		place = 'Last Place';
run;



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 6 replies
  • 592 views
  • 0 likes
  • 4 in conversation