Help using Base SAS procedures

need 'min' function to return variable name and not observation values

Reply
New Contributor
Posts: 2

need 'min' function to return variable name and not observation values

Hi everybody! I am new here, I am a doctor and I use SAS to perform basic stat analysis. Right now I am having trouble editing a dataset, I am not sure how to solve the problem but I know there must be an easy way to do it, so I am asking for your help Smiley Happy

This is a sample of my dataset

id      exam1      exam2    exam3        exam4

1       1999        2007      2003          2000

2       2009        1985      2004          2001 

3       2000        2001      2002          2003

What I want to do: to create a new variable 'firstexam' in which i have the name of the first exam and not the actual date

my new dataset should be like this:

id   ...   firstexam

1          exam1

2          exam2

3          exam1

if i use the min function I get

id   ...    firstexam

1             1999

2             1985

3             2000

I know I could do it with a bunch of 'if' and 'then' but the editor would be so long and ugly.. I am sure there is an easier way to get the job done...

Thanks a lot!

Trusted Advisor
Posts: 1,321

need 'min' function to return variable name and not observation values

Hi Michele,

I haven't tested this but I think it might be what you are after... use the vname function around your min function...

firstexam = vname(min(exam1, exam2, exam3, exam4));

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202943.htm

Hope this helps.

Cheers,

Michelle

PROC Star
Posts: 7,492

need 'min' function to return variable name and not observation values

Posted in reply to MichelleHomes

I think you are looking for something like:

data have;

  input id      exam1      exam2    exam3        exam4;

  array years(*) exam1-exam4;

  firstexam=vname(years[whichn(min(of years(*)), of years

  • )]);
  •   cards;

    1       1999        2007      2003          2000

    2       2009        1985      2004          2001

    3       2000        2001      2002          2003

    ;

    run;

    proc print;

    run;

    Regular Contributor
    Posts: 184

    need 'min' function to return variable name and not observation values

    Are ties possible (2 or more tests in the earliest year for an ID)? If so, what should materialize in FIRSTEXAM?

    New Contributor
    Posts: 2

    need 'min' function to return variable name and not observation values

    Thanks everyone.

    Ties will not be possible, as the date would actually be composed of year-month-day, I have only put years to simplify it.

    I will try the array+vname approach as soon as I can and I will let you know.

    PROC Star
    Posts: 7,492

    need 'min' function to return variable name and not observation values

    You don't have to change anything in the code .. you just have to ensure that your date fields were in fact read in as SAS dates.

    Years is simply the name I assigned to the array, thus you don't really have to change it.  If you do want it to reflect something else, make sure that you change it in all four instances where it is used.

    Ask a Question
    Discussion stats
    • 5 replies
    • 182 views
    • 1 like
    • 4 in conversation