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

data want;
set have;

day=substr(birthdate,1,1);
month=substr(birthdate,2,1);
year=substr(birthdate,7,1);

if day eq "-" and month eq "-" then year=substr(birthdate,3,4);
else if day eq "-" and month ne "-" then year=substr(birthdate,6,4);
else if year eq "-" then year="0000";
else if month eq "-" then year=substr(birthdate,5,4);


if day eq "-" then day="01";
else if day ne "-" then do;
day=substr(birthdate,1,2); /*Day*/
month=substr(birthdate,3,1);
end;

if month eq "-" then month="Jan";
else if month ne "-" then month=substr(birthdate,2,3);

converted_date=day||"-"||month||"-"||year;
run;
but there is a issue my output has some missing values--> here is my output data


birthdate
day
month
year
converted_date
1 -Jan-1975 01 Jan 1975 01 -Jan-1975
2 --1977 01 Jan 1977 01 -Jan -1977
3 02--1978 02 Jan 7 02 -Jan -7
4 03-Jan- 03 Jan 0000 03 -Jan -0000

in the 3 observation the year isn't getting displayed so whats the issue i can't solve & the code has zero errors
ballardw
Super User

Part of your issue is you haven't broken the code up to identify all of your possible cases. 1) all values missing, 2) all but day missing, 3) all but month missing, 4) all but year missing, 5) ony missing day, 6) only missing month, 7) missing only year and finally 😎 missing no values.

 

For instance with your third record and the value of 02--1978

None of this code gets executed:

if day eq "-" and month eq "-" then year=substr(birthdate,3,4);
else if day eq "-" and month ne "-" then year=substr(birthdate,6,4);
else if year eq "-" then year="0000";
else if month eq "-" then year=substr(birthdate,5,4);

because "day" = 0, "month"=2 and "year"=7

 

And for the case where day is ne - and month ne - you never re-read the year so you get that single digit.

 

I'm answering this away from my SAS install and have an idea I can't test at this time that may be much simpler over all.

 

 

 

 

RTelang
Fluorite | Level 6
@ballard @RW9 thanks solved the issue..

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