BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

I have a field called reporting_dt_bin shown as a character. It is a concatenate of reporting_Yr and reporting month, both of which were originally dates

Reporting_dt_Bin = (Reporting_year_Bin || '-' || Reporting_month_txt); produces this output example

 

Reporting_dt_Bin

15- 3

15- 4

15- 5

15- 6

15- 7

 

Is there a way to maintain the reporting_dt_bin format and create another format that converts this into a date

Example

Reporting_dt_bin2

Mar15

Apr15

May15

The date format would be monyy5.

5 REPLIES 5
FreelanceReinh
Jade | Level 19

There may be more elegant solutions, but this should do what you want, if the year and month variables are still available:

data have;
length Reporting_dt_bin $5;
Reporting_year_Bin='15';
do m=3 to 7;
  Reporting_month_txt=put(m,2.);
  Reporting_dt_Bin = (Reporting_year_Bin || '-' || Reporting_month_txt);
  output;
end;
drop m;
run;

proc print width=min;
run;


data want;
set have;
length Reporting_dt_bin2 $5;
Reporting_dt_bin2=propcase(put(input(cats(Reporting_year_Bin,put(input(Reporting_month_txt,2.),z2.)), yymmn4.),monyy5.));
run;

proc print width=min;
run;

If only Reporting_dt_Bin is available, you could resort to this:

data have;
length Reporting_dt_bin $5;
do m=3 to 7;
  Reporting_dt_bin='15-'||put(m,2.);
  output;
end;
drop m;
run;

proc print width=min;
run;


data want;
set have;
length Reporting_dt_bin2 $5;
Reporting_dt_bin2=propcase(put(input(cats(scan(Reporting_dt_bin,1,'-'),put(input(scan(Reporting_dt_bin,2,'-'),2.),z2.)), yymmn4.),monyy5.));
run;

proc print width=min;
run;

If you are happy with MAR15 etc. (this is actually MONYY5. format) instead of Mar15, you can omit PROPCASE.

Steelers_In_DC
Barite | Level 11

Here is another solution:

 

data have;
infile cards dsd;
input Reporting_dt_Bin$;
cards;
15- 3
15- 4
15- 5
15- 6
15- 7
;

proc format;
    value mon 1 = JAN
              2 = FEB
              3 = MAR
              4 = APR
              5 = MAY
              6 = JUN
              7 = JUL
              8 = AUG
              9 = SEP
              10= OCT
              11= NOV
              12= DEC
;

data want;
set have;
Reporting_dt_Bin2 = input(cats(scan(Reporting_dt_Bin,1,'-'),put(input(scan(Reporting_dt_Bin,2,'-'),8.),mon.),input(put(year(today()),4.),$4.)),date9.);
format Reporting_dt_Bin2 monyy5.;
run;

 

This takes the character input you have, breaks it up and formats it as DDMMMYYYY.  The year is based on today().

Patrick
Opal | Level 21

And another coding option:

%let rx='s/(\d+)[^\d]+(\d+).*/\1-\2-01/';

data sample;
  input Reporting_dt_Bin $5.;
  format Reporting_dt monyy5.;
  Reporting_dt=input(prxchange(&rx,1,Reporting_dt_Bin),yymmdd8.);
  datalines;
5-10
5- 3
15-10
15- 3
15- 4
15- 5
15- 6
15- 7
;
run;
Tom
Super User Tom
Super User

SAS does not have an INFORMAT for converting values in YYMM (or YY-MM) format to dates. But if you add an '-1' to the end you can use the YYMMDD informat to convert that string to a date. You could then attach the MONYY format to it.  Or use the MONYY format to convert it to a text string.  Also note that the YYMMDD format does not like the space after the hyphen in the example strings you posted.  You can use the COMPRESS() function to remove the spaces.

data have;
  input reporting_dt_bin $5.;
  list;
cards;
15- 3
15- 4
15- 5
15- 6
15- 7
;
data want ;
   set have;
   date=input(cats(compress(reporting_dt_bin,' '),'-01'),yymmdd8.);
   format date monyy5.;
run;

Note that you could generate the REPORTING_DT_BIN without the spaces if you used:

Reporting_dt_Bin = catx('-',Reporting_year_Bin,Reporting_month_txt);

If you had the month as a number you could use the Z2 format to build values that have the leading zero when month is less than 10.  This would have the advantage of both working with the YYMMDD informat and also allowing REPORTING_DT_BIN text string to sort properly.

Reporting_dt_Bin = catx('-',Reporting_year_Bin,put(input(Reporting_month_txt,2.),Z2.));

 

 

Patrick
Opal | Level 21

@Tom  "SAS does not have an INFORMAT for converting values in YYMM (or YY-MM) format to dates"

 

Actually there is informat YYMMN which can process such strings (not with a dash though but blanks as separator seem to work).

 

I prefere the solution you've posted so below just for "completeness":

data sample;
  input Reporting_dt_Bin $5.;
  format Reporting_dt monyy5.;
  Reporting_dt=input(compbl(translate(Reporting_dt_Bin,' ','- ')),yymmn.);
  datalines;
5-10
5- 3
15-10
15- 3
15- 4
15- 5
15- 6
15- 7
;
run;

 

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!

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
  • 5 replies
  • 1265 views
  • 1 like
  • 5 in conversation