BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

4 REPLIES 4
knveraraju91
Barite | Level 11

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

Kurt_Bremser
Super User

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 4201 views
  • 3 likes
  • 3 in conversation