DATA Step, Macro, Functions and more

How to read a date value with partial dates

Reply
Super Contributor
Posts: 272

How to read a date value with partial dates

Dear,

 

I am running following program. i am not getting the out I need. please suggest. Thank you

 

data one;
input cmendtc_raw $11.;
datalines;
UN/Feb/2015
;
data two;
set one;
cmendtc=put(input(cmendtc_raw, ?? date11.),yymmdd10.);

if missing(cmendtc) and not missing(cmendtc_raw) then
do;
length _day1 _month1 _year1 8;
_day1 =input(scan(cmendtc_raw,1,'/','m'), ?? best32.);
_month1=month(input(scan(cmendtc_raw,2,'/','m')||'2000', ?? monyy.));
_year1 =input(scan(cmendtc_raw,3,'/','m'),?? best32.);
end;
if _day1<1 or _day1>31 then call missing(_day1);
if _month1<1 or _month1>12 then call missing(_month1);

/* valid day and year? */
if mdy(01,_day1,_year1) then cmendtc=put(_year1,z4.)||'--'||put(_day1,z2.);

/* valid month and year? */
else if mdy(_month1,01,_year1) then cmendtc=put(_year1,z4.)||'-'||put(_month1,z2.);

/* valid year? */
else if mdy(02,01,_year1) then cmendtc=put(_year1,z4.);
run;

 

output need:

2015-02

 

I am seeing a blank value     "         .'   . The missing function is not recognizing. Please help. Thank you

Super Contributor
Posts: 272

How to read date value with partial date

Dear,

 

I am running following program. i am not getting the out I need. please suggest. Thank you

 

data one;
input cmendtc_raw $11.;
datalines;
UN/Feb/2015
;
data two;
set one;
cmendtc=put(input(cmendtc_raw, ?? date11.),yymmdd10.);

if missing(cmendtc) and not missing(cmendtc_raw) then
do;
length _day1 _month1 _year1 8;
_day1 =input(scan(cmendtc_raw,1,'/','m'), ?? best32.);
_month1=month(input(scan(cmendtc_raw,2,'/','m')||'2000', ?? monyy.));
_year1 =input(scan(cmendtc_raw,3,'/','m'),?? best32.);
end;
if _day1<1 or _day1>31 then call missing(_day1);
if _month1<1 or _month1>12 then call missing(_month1);

/* valid day and year? */
if mdy(01,_day1,_year1) then cmendtc=put(_year1,z4.)||'--'||put(_day1,z2.);

/* valid month and year? */
else if mdy(_month1,01,_year1) then cmendtc=put(_year1,z4.)||'-'||put(_month1,z2.);

/* valid year? */
else if mdy(02,01,_year1) then cmendtc=put(_year1,z4.);
run;

 

output need:

2015-02

 

I am seeing a blank value     "         .'   . The missing function is not recognizing. Please help. Thank you

Super User
Posts: 6,936

Re: How to read date value with partial date

Why so complicated?

data have;
input cmendtc_raw $11.;
datalines;
UN/Feb/2015
;
run;

proc format;
value $monthrev
  'Jan' = '-01'
  'Feb' = '-02'
  'Mar' = '-03'
/* add other months here */
  other = '   '
;
run;

data want;
set have;
length cmendtc $10;
cmendtc = scan(cmendtc_raw,3,'/') !! put(scan(cmendtc_raw,2,'/'),$monthrev3.);
if substr(cmendtc_raw,1,2) ne 'UN' then cmendtc = trim(cmendtc) !! substr(cmendtc_raw,1,2);
run;

proc print data=want noobs;
run;

Result:

cmendtc_raw    cmendtc

UN/Feb/2015    2015-02
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,401

Re: How to read date value with partial date

Or without formats;

data want (drop=mnth);
  length dstr $20;
  input dstr $;
  dstr=tranwrd(dstr,"UN","");
  if scan(dstr,2,"/") ne "" then mnth=put(month(input(cats("01",scan(dstr,2,"/"),"2010"),date9.)),z2.);
  formatted=catx("-",scan(dstr,3,"/"),mnth,scan(dstr,1,"/"));
datalines;
UN/Feb/2016
01/Mar/2014
UN/UN/2011
;
run;
Super User
Posts: 6,936

Re: How to read a date value with partial dates

[ Edited ]

And reread https://communities.sas.com/t5/Base-SAS-Programming/How-to-convert-date-to-character-dates-when-sour...

There were a lot of good hints and suggestions there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 151 views
  • 3 likes
  • 3 in conversation