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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1184 views
  • 0 likes
  • 2 in conversation