BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

 

3 REPLIES 3
collinelliot
Barite | Level 11

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
;
art297
Opal | Level 21

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

 

ChrisNZ
Tourmaline | Level 20

mdy(01,01,1960) generates 0.

change 01 to 02, for example:

      else if mdy(02,01,_year) then want_str=put(_year,z4.);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 810 views
  • 3 likes
  • 4 in conversation