BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
richart
Fluorite | Level 6

There has to be a simpler solution to this and I am not sure why I'm having so much difficulty. I have two date vars, with date9. format. I simply want to create a new date variable in the same data step based on these two variables, but can't without it converting to numeric (or character if I try and use 'put' statements but then my 'format new_date date9.' step doesn't work . help?

 

I'm trying: 

 

data data1;
set data;
if date1^='' and date2^='' then new_date=date1;
else if date2='' then new_date=date1;
run; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
What does ' my 'format new_date date9.' step doesn't work' mean?

Post the log.

Since you're using "" to check for missing, that would imply that your date variables are character not numeric and you cannot apply a numeric format to a character variable. In this case you need to also convert the dates to numerics variables.

Is your first condition correct? It checks that both date1 and date2 are not missing and then assigns date1. Then the ELSE checks if DATE2 is missing and assigns Date1? Both essentially assign the Date1 value to DATE.

Also, is this homework? If not use COALESCE/COALESCEC instead.

View solution in original post

3 REPLIES 3
Reeza
Super User
What does ' my 'format new_date date9.' step doesn't work' mean?

Post the log.

Since you're using "" to check for missing, that would imply that your date variables are character not numeric and you cannot apply a numeric format to a character variable. In this case you need to also convert the dates to numerics variables.

Is your first condition correct? It checks that both date1 and date2 are not missing and then assigns date1. Then the ELSE checks if DATE2 is missing and assigns Date1? Both essentially assign the Date1 value to DATE.

Also, is this homework? If not use COALESCE/COALESCEC instead.

richart
Fluorite | Level 6
sorry and thanks... I think me forgetting to use . instead of '' for the date vars messed everything up. I now do this and it works:

data data1;
set data;
if date1^=. and date2^=. then new_date=date1;
else if date2=. then new_date=date1;
format new_date date9.;
run;
Reeza
Super User
Glad it worked and hope that logic is correct.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 654 views
  • 0 likes
  • 2 in conversation