+ Reply to Thread
Results 1 to 3 of 3

Thread: retain or multiple by variables?

  1. #1
    Points: 9,051, Level: 64
    Level completed: 1%, Points required for next Level: 299

    Posts
    482
    Thanks
    22
    Thanked 3 Times in 3 Posts

    retain or multiple by variables?



    Hi!

    I have a situation where the data look like the following:

    Code: 
    Student      Teacher     Grade
    John               1              15
    John               2              14
    John               3              14
    John               4              14
    Sara               3              12
    Sara               4              12
    Matt               1              19
    Jane               4              12
    Jane               1              12
    So here I have a dataset of Students who took a test with different teachers. My aim is to extract the highest grade achieved for each student, and the accompanying teacher. In the dataset, It's possible to have ties on the highest grades. In this case, I'd just like to make a new variable called 'flag' and give it a value of 'T'. For these students the Grade and the Teacher is irrelevant - I'm only interested in knowing that there was a tie, if the tie is the highest grade.

    So for example, my output dataset (based on the above) would look like the following.


    Code: 
    Student      Teacher     Grade    Flag
    John               1              15       .
    Sara               4              12       T
    Matt               1              19       .
    Jane               1              12       T
    I've been playing around with retain statements and have looked into using multiple by variables (i.e, first.student, first.teacher) and I think retain is the way to go. I can't exactly shown what I've attempted as the code is located on the unix. Could anyone offer some tips for writing code to perform this task?

  2. #2
    Points: 2,180, Level: 28
    Level completed: 20%, Points required for next Level: 120

    Location
    Chicago, IL
    Posts
    108
    Thanks
    1
    Thanked 19 Times in 15 Posts

    Re: retain or multiple by variables?

    You could do this w/ prog sql and probably in fewer steps, but this will work:

    Code: 
    data test;
    input
    Student $      Teacher     Grade;
    datalines;
    John               1              15
    John               2              14
    John               3              14
    John               4              14
    Sara               3              12
    Sara               4              12
    Matt               1              19
    Jane               4              12
    Jane               1              12
    run;
    
    proc sort data=test out=test2; by student descending grade; run;
    
    data test3 test4; set test2; by student;
    if first.student then do; output test3; end;
    else do; output test4; end;
    run;
    
    proc sort data=test3; by student grade; run;
    proc sort data=test4; by student grade; run;
    
    data test5;
    merge test3 (in=a) test4 (in=b);
    by student grade;
    if a;
    if a and b then flag='T'; else flag=' ';
    run;

  3. The Following User Says Thank You to Janus For This Useful Post:

    jamesmartinn (10-22-2012)

  4. #3
    Points: 9,051, Level: 64
    Level completed: 1%, Points required for next Level: 299

    Posts
    482
    Thanks
    22
    Thanked 3 Times in 3 Posts

    Re: retain or multiple by variables?


    Sorry for the delay in replying. Thank you so much, this worked great.

    Cheers,

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats