DATA Step, Macro, Functions and more

How to read the date value when it unknown characters and 1960 or 0000

Reply
Super Contributor
Posts: 272

How to read the date value when it unknown characters and 1960 or 0000

Dear,

 

In my data the following date values  are present. The code below worked fine for all OBS containing similar values except for second OBS.

 

The output for the first OBS I am getting 0000 and for the second OBS it is blank. For the third one i am getting 2000.

 

Please help in my code to read when year =1960. Thank you.

 

data1

 

OBS       have_str

   1         UN/UNK/0000

    2       UN/UNK/1960

    3        UN/UNK/2000

 

code:

options missing=' ';
data want;
  set have;
  length want_str $11.;

  /* valid day, month and year? */
  want_str=put(input(have_str, ?? date11.),yymmdd10.);

  if missing(want_str) and not missing(have_str) then
    do;
      length _day _month _year 8;
      _day  =input(scan(have_str,1,'/','m'), ?? best32.);
      _month=month(input(scan(have_str,2,'/','m')||'2000', ?? monyy.));
      _year =input(scan(have_str,3,'/','m'),?? best32.);

      if _day<1 or _day>31 then call missing(_day);
      if _month<1 or _month>12 then call missing(_month);

      /* valid day and year? */
      if mdy(01,_day,_year) then want_str=put(_year,z4.)||'--'||put(_day,z2.);

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

      /* valid year? */
      else if mdy(01,01,_year) then want_str=put(_year,z4.);

    end;
run;

OUTPUT getting:

 

data2

OBS               want_str 

1                       0000

2                        blank

3                        2000

 

output need:

OBS         want_str

1              blank

2              1960

3               2000

 

PROC Star
Posts: 290

Re: How to read the date value when it unknown characters and 1960 or 0000

Maybe this?

 

data want;
input str :$12.;
    want_str = prxchange('s/0000/blank/', 1, scan(str, -1, '/'));
datalines;
UN/UNK/0000
UN/UNK/1960
UN/UNK/2000
;
PROC Star
Posts: 7,364

Re: How to read the date value when it unknown characters and 1960 or 0000

Your problem, the way you approached the solution, is that mdy will interpret 0 as year 2000 and Jan 1st, 1960 is sas date 0;

 

As an alternative try:

 

data want;
  input str :$12.;
  want_str = ifc(input(scan(str, -1, '/'),4.) lt 1800,' ',scan(str, -1, '/'));
  datalines;
UN/UNK/0000
UN/UNK/1960
UN/UNK/2000
;

HTH,

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 1,566

Re: How to read the date value when it unknown characters and 1960 or 0000

mdy(01,01,1960) generates 0.

change 01 to 02, for example:

      else if mdy(02,01,_year) then want_str=put(_year,z4.);
Ask a Question
Discussion stats
  • 3 replies
  • 115 views
  • 3 likes
  • 4 in conversation