Help using Base SAS procedures

partial dates

Reply
Contributor
Posts: 44

partial dates

Hey,

can you tell me how i can manage partial dates in SAS? I would like to import a csv-file with a date-variable (the format in the csv-file is like: 10102000 or 1965 or 091922).

How can i 'tell' SAS, that 10102000 should have the format ddmmyyyy, 091922 the format mmyyyy and 1965 yyyy?? My idea was to import the variable as character-format and then ask with the length-function for the value-length and make 3 new variables for each format, like:

if length(birthdate)EQ 8 Then do;
test=input(birthdate,ddmmyy10.);
format test ddmmyy10.;
end;

if length(birthdate)EQ 6 Then do;
test2=input(birthdate, mmyy7);
format test2 mmyy7.;
end;

if length(birthdate) EQ 4 Then do;
test3=input(birthdate,year.);
format test3 year.;
end;

for test everythings works fine but for test2 and test3 not Smiley Sad do you have any idea?

thanks in advance!

Marie
Super Contributor
Super Contributor
Posts: 3,174

Re: partial dates

When possible, it's best to consider appending character constants to make up the missing component, when converting data-strings to SAS DATE variables. Have a look at the ANYDTDTE INFORMAT, however that can potentially yield undesirable date values.

Scott Barry
SBBWorks, Inc.


37 data _null_;
38 yyyy = '1965';
39 dt = input('0101'!!yyyy,ddmmyy8.);
40 put dt= date9.;
41 run;

dt=01JAN1965
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Contributor
Posts: 44

Re: partial dates

thank you, yes i think i have to do it like that...


do you have also an idea how i can code 06.06.6666 and 07.07.7777 as special missing?
Super Contributor
Super Contributor
Posts: 3,174

Re: partial dates

I really don't use "special missing" assignment. If an assignment statement (using the INPUT function you show with a date) yields a missing value, then you have an invalid formatted date string, and then you can take some action -- suggest reviewing the SAS DOC and the SAS support website. Always reset _ERROR_=0; when you have this condition.

Scott Barry
SBBWorks, Inc.


Suggested Google advanced search arguments, this topic / post:

+"special missing" site:sas.com

input function "_error_" site:sas.com
Valued Guide
Posts: 2,177

Re: partial dates

please remove partial post Message was edited by: Peter.C
Valued Guide
Posts: 2,177

Re: partial dates

to define your own special-missings, use
PROC FORMAT ;
invalue my_mis
'06.06.6666' = .D /*almost number of the be... */
'07.07.7777' = .S
other =[ anydtdte. ]
;
run ;
Then use the informat MY_MIS. instead of ANYDTDTE.
Respected Advisor
Posts: 4,173

Re: partial dates

Hi Marie

The code below creates a variable containing a SAS date.

data have;
datestring='10102000';
output;
datestring='091922';
output;
datestring='1965';
output;
run;

data want;
set have;
SASDateVar=input(datestring,?? ddmmyy8.);
if missing(SASDateVar) then SASDateVar=input(cats('01',datestring),?? ddmmyy8.);
if missing(SASDateVar) then SASDateVar=input(cats('0101',datestring),?? ddmmyy8.);
put SASDateVar= date9.;
SASDateVarAligned=intnx('year',SASDateVar,0,'B');
put SASDateVarAligned= date9.;
run;

HTH
Patrick
Ask a Question
Discussion stats
  • 6 replies
  • 877 views
  • 0 likes
  • 4 in conversation