🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@ballard @RW9 thanks solved the issue..
- « Previous
-
- 1
- 2
- Next »