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;
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 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.
Hi, since the BY grouping is done by a single variable, did you mean find the second observation?
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: