BookmarkSubscribeRSS Feed
weiwei2392
Fluorite | Level 6

data.PNG

 

Hi all the experts, 

How do I find the earliest date within dateofadmin, dateofdrug and dateofmed for each ID using SAS code?

(ex. the earliest date for ID110 is dateofadmin14842  ((14842<18390 and ignore the "."))    )

and then i want to put the earliest in a new column :S 

the date has been transfer into numeric format already. 

However, i couldnt find the ways to compare numbers within columns :S

Please kindly give me a hint , would really be appreciated!Cat Mad

 

6 REPLIES 6
Shmuel
Garnet | Level 18

data want;

set have;

      firstdate = .;

      array datex {3} dateofadmin dateofdrug dateofmed;

      do i=1 to 3;

               if datex(i) ne . then do;

                  if firstdate = .  then firstdate = datex(i);

                  else if datex(i) < firstdate  then firstdate = datex(i);

     end; end;

run;

Astounding
PROC Star

The MIN function should ignore missing values:

 

earliest_date = min(dateofadmin, dateofdrug, dateofmed);

PGStats
Opal | Level 21

The min function accepts any number of arguments.

 

data want;
set have;
firstdate = min(dateofadmin, dateofdrug, dateofmed);
run;
PG
weiwei2392
Fluorite | Level 6

thank you so much!

Now i have one more question

what if i wanna add one more column "where" is states where does the first date comes from?

 

like, 

擷取.PNG

 

thank you 🙂

Shmuel
Garnet | Level 18

data want;

set have;

      firstdate = .;

      array datex {3} dateofadmin dateofdrug dateofmed;

      do i=1 to 3;

               if datex(i) ne . then do;

                  if firstdate = .  then do;

                      firstdate = datex(i);

                      where = vname(datex(i));  

                  end;

                  else if datex(i) < firstdate  then firstdate = datex(i);

     end; end;

run;

Astounding
PROC Star

It's possible to shorten the code by a line or two using arrays, but this variation might be easier to understand.

 

data want;

set have;

earliest_date = min(dateofadmin, dateofdrug, dateofmed);

length varname $ 32;

if earliest_date > . then select (whichn(earliest_date, dateofadmin, dateofdrug, dateofmed));

   when (1) varname='dateofadmin';

   when (2) varname='dateofdrug';

   when (3) varname='dateofmed';

end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 648 views
  • 4 likes
  • 4 in conversation