BookmarkSubscribeRSS Feed
marieK
Obsidian | Level 7
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 😞 do you have any idea?

thanks in advance!

Marie
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
marieK
Obsidian | Level 7
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?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Peter_C
Rhodochrosite | Level 12
please remove partial post Message was edited by: Peter.C
Peter_C
Rhodochrosite | Level 12
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.
Patrick
Opal | Level 21
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 2808 views
  • 0 likes
  • 4 in conversation