BookmarkSubscribeRSS Feed
claremc
Obsidian | Level 7

Hello, 

 

I am trying to create a new date variable that is equal to multiple date variables in SAS (depending on missingness). The existing variables (date_lastschind, date_sx_ind, and date_covidcoll_ind in the below example) are already in mmddyy10. format. The following code is not creating the new variable, "event date" in the mmddyy10 format that I want it in. Does anyone know why? 

data dash; 
set dash; 
if date_sx_ind = . and date_covidcoll_ind = . and date_lastsch_ind ne . then event_date = date_lastsch_ind;
if (date_sx_ind<=date_covidcoll_ind) or date_covidcoll_ind = . and date_sx_ind ne . then event_date = date_sx_ind; 
if (date_sx_ind>date_covidcoll_ind) or date_sx_ind = . and date_covidcoll_ind ne . then event_date = date_covidcoll_ind; 
if date_lastsch_ind = . and date_covidcoll_ind ne . then event_date = date_covidcoll_ind; 
if date_lastsch_ind = . and date_sx_ind ne . then event_date = date_sx_ind; 
format event_date mmddyy10.; 
run; 

Thanks, 

Clare

6 REPLIES 6
ballardw
Super User

Some data would be nice.

And and example of what you are expecting the result to look like.

 

"equal to multiple" variables is almost nonsense without detailed context and likely not possible with anything except character variable results.

 

With multiple If statements all assigning values to the same variable the only result would for the last true condition.

 

Note that use of the

Data <datasetname>;

   set <datasetname>;

construct will completely replace the data set. So logic error results can exist from a previous test. If you previously ran this code and accidentally created event_date as character then you can't assign a numeric format.

What does the log look like when you run the code, though is suggest the next run has the output data set different. Copy the log text of the code and any notes, warnings or errors and paste into a text box opened on the forum with the </> icon. This will preserve the text format and help keep any of the SAS diagnostic messages formatted properly.

Astounding
PROC Star

How is the logic you want any different from this?

event_date = min(date_sx_ind, date_covidcol1_ind, date_lastsch_ind);

If this doesn't do the trick, perhaps you could explain where it falls down.

 

 

claremc
Obsidian | Level 7

Hi, here is a picture of my log. I used your code above too. Event date is being created in numerical format but the mmddy10 format still doesn't work.

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         data dash2;
 74         set dash;
 75         event_date = min(date_sx_ind, date_covidcol1_ind, date_lastsch_ind);
 76         format event_date mmddyy10.;
                              _________
                              484
 NOTE 484-185: Format $MMDDYY was not found or could not be loaded.
ballardw
Super User

@claremc wrote:

Hi, here is a picture of my log. I used your code above too. Event date is being created in numerical format but the mmddy10 format still doesn't work.

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         data dash2;
 74         set dash;
 75         event_date = min(date_sx_ind, date_covidcol1_ind, date_lastsch_ind);
 76         format event_date mmddyy10.;
                              _________
                              484
 NOTE 484-185: Format $MMDDYY was not found or could not be loaded.

That Note about the format means that your Event_date variable is character. So that is something else to address before you get to this point if you want to manipulate or display dates. Which brings up questions about other "date" variables as well. If they are character then results of comparisons such as MIN, <, > , Max and others can be other than expected. Quick, is this comparison true (character values)

"3/15/2020" < "11/12/2020"?

Tom
Super User Tom
Super User

EVENT_DATE must already be defined as character in the source dataset DASH.

Use a different name for your new numeric variable.

s_lassen
Meteorite | Level 14

I think you need to clean up your code a bit, first of all.

 

As you are not using any ELSE statements, what happens is this:

  1. All the conditions are tested, which is probably a waste of computer time
  2. The last IF statements takes precedence, meaning that if that they are executed, all of what you did before does not matter.

So, actually the code you have shown is equivalent to:

data dash; 
  set dash; 
  if date_lastsch_ind = . and date_sx_ind ne . then 
    event_date = date_sx_ind; 
  else if date_lastsch_ind = . and date_covidcoll_ind ne . then 
    event_date = date_covidcoll_ind; 
  else if (date_sx_ind>date_covidcoll_ind) or date_sx_ind = . and date_covidcoll_ind ne . then 
    event_date = date_covidcoll_ind; 
  else if (date_sx_ind<=date_covidcoll_ind) or date_covidcoll_ind = . and date_sx_ind ne . then 
    event_date = date_sx_ind; 
  else if date_sx_ind = . and date_covidcoll_ind = . and date_lastsch_ind ne . then 
    event_date = date_lastsch_ind;
format event_date mmddyy10.; 
run; 

But I assume that what you wanted to do was something like your original code, but with ELSE statements.

So I think you will have to supply a text description of what it is you want, or perhaps some example data (as datastep code) showing what you have and what you want from that.

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1526 views
  • 0 likes
  • 5 in conversation