DATA Step, Macro, Functions and more

Reading one date from two numeric formats

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Reading one date from two numeric formats

I have an outpatient dataset with a variable "FRDATE" that is formatted numeric 8. Each observation has either 7 numbers (ddmyyyy) or 8 numbers (ddmmyyyy). How do I convert this numeric field to a unified date field?

 

Ex:  Patient  FRDATE

        Obs 1     2162016

        Obs 2   21122016

 

Thanks!


Accepted Solutions
Solution
‎05-08-2018 11:53 AM
Super User
Posts: 13,321

Re: Reading one date from two numeric formats

Posted in reply to MDH_sasuser

Is that first "date" supposed to be Feb 16 2016 or June 21 2016?

 

Did the original data brought into SAS actually have leading zeros such that the first example was 02162016, which would maybe clear up the above question?

If so perhaps:

data have;
   input frdate;
   sasdate= input(put(frdate,z8.-L),anydtdte.);
   format sasdate mmddyy10.;
datalines;
2162016
 21122016
;
run;

The line is the SASDATE= which reads in a text version of your date with a leading 0 using an "any date" informat  (anydtdte.)

 

View solution in original post


All Replies
Solution
‎05-08-2018 11:53 AM
Super User
Posts: 13,321

Re: Reading one date from two numeric formats

Posted in reply to MDH_sasuser

Is that first "date" supposed to be Feb 16 2016 or June 21 2016?

 

Did the original data brought into SAS actually have leading zeros such that the first example was 02162016, which would maybe clear up the above question?

If so perhaps:

data have;
   input frdate;
   sasdate= input(put(frdate,z8.-L),anydtdte.);
   format sasdate mmddyy10.;
datalines;
2162016
 21122016
;
run;

The line is the SASDATE= which reads in a text version of your date with a leading 0 using an "any date" informat  (anydtdte.)

 

New Contributor
Posts: 2

Re: Reading one date from two numeric formats

Thank you all so much for the quick replies and solutions. This agency sent me 5 years to merge, each year in a a text file with a different delineation and data dictionary... my eyes are crossing 

Contributor
Posts: 41

Re: Reading one date from two numeric formats

Posted in reply to MDH_sasuser

Hi you can try like this 

 

data date;
input patient$ date;
cards;
Obs_1 2162016
Obs_2 21122016
;
run;

data date1;
set date;
date1=input(date,8.);
format date1 ddmmyy10.;
run;

Trusted Advisor
Posts: 1,831

Re: Reading one date from two numeric formats

Posted in reply to MDH_sasuser

 

You can check length of the char type var:

 

length temp_date $8;
temp_date = put(frdate, 8.); if length(temp_date) = 7 then tempdate = cat(substr(temp_date,1,2) , '0' , substr(temp_date,3,5) ); frdtae = input(temp_date, ddmmyy8.);

  

 

Super User
Posts: 6,632

Re: Reading one date from two numeric formats

[ Edited ]
Posted in reply to MDH_sasuser

I think you're omitting a case here.  Couldn't January 1, 2016 be represented as 6 digits?

 

Anyway, taking your rule that these are actually numeric variables, I would create a character version that contains 8 digits.  For example:

 

if frdate > 10000000 then char_version = put(frdate, 8.);

else if frdate > 1000000 then do;

   frdate = 10 * int(frdate / 10000) + mod(frdate, 10000);

   char_version = put(frdate, 8.);

end;

else if frdate > 100000 then do;  /* to handle 112016 = January 1, 2016 */

   frdate = 100 * int(frdate / 10000) + mod(frdate, 10000);

   char_version = put(frdate, 8.);

end;

 

Finally, convert that character version to a valid SAS date.

 

fr_sasdate = input(put(char_version, z8.), ddmmyy8.);

format fr_sasdate yymmdd10.;  /* or pick some other format that you prefer */

 

The code is untested at the moment, so I may tweak it.

 

 

***************** EDITED:

 

Upon review of the results of this code, there are more problems than expected.  For example, how do you know what date this is:

 

1112016

 

SAS will make the choice for you, but how will you know that SAS did the right thing?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 114 views
  • 0 likes
  • 5 in conversation