BookmarkSubscribeRSS Feed
michele
Calcite | Level 5

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!

5 REPLIES 5
MichelleHomes
Meteorite | Level 14

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

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
art297
Opal | Level 21

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;

    Howles
    Quartz | Level 8

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

    michele
    Calcite | Level 5

    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.

    art297
    Opal | Level 21

    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.

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 5 replies
    • 1062 views
    • 1 like
    • 4 in conversation