DATA Step, Macro, Functions and more

How to read the Date in yymm format

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

How to read the Date in yymm format

Hello Expert,

 

I have date data like yymm format storing character datatype.

first two letter represent year(yy) and 3rd-4th digit represent month(mm).

I want to add date (01) in evey row  and want to show my date in dd mm yy format in char data type.

How can we do it, Please help me.

Source data:

Trans_dt

-------------

1705

1811

2101

 

Output

Trans_dt

----------------

01-05-2017

01-11-2018

01-01-2021


Accepted Solutions
Solution
‎05-19-2017 10:36 PM
Trusted Advisor
Posts: 1,400

Re: How to read the Date in yymm format

Read the date as $4 then create the sas numeric date:

 

data have;

  input datex $4.;

  date = input(datex||'01' , yymmdd8.);

  format date ddmmyy10.;

  drop datex;

cards;

1705

;

run;

View solution in original post


All Replies
Solution
‎05-19-2017 10:36 PM
Trusted Advisor
Posts: 1,400

Re: How to read the Date in yymm format

Read the date as $4 then create the sas numeric date:

 

data have;

  input datex $4.;

  date = input(datex||'01' , yymmdd8.);

  format date ddmmyy10.;

  drop datex;

cards;

1705

;

run;

PROC Star
Posts: 7,364

Re: How to read the Date in yymm format

I, personally, think it would be more advantageous to store the date as a SAS date and include a format in order to have it appear in the format you want. Regardless, here is how to do it both ways:

data have;
  input cdate $;
  date=mdy(substr(cdate,3),1,substr(cdate,1,2));
  format date ddmmyyd10.;
  date_as_char=put(date,ddmmyyd10.);
  cards;
1705
1811
2101
;

Art, CEO, AnalystFinder.com

Trusted Advisor
Posts: 1,630

Re: How to read the Date in yymm format

[ Edited ]

UNTESTED CODE

 

data in;
    input value $;
    yy=substr(value,1,2)+0;
    mm=susbtr(value,3,2)+0;
    date=mdy(mm,1,yy);
    format date ddmmyyd10.;
cards;
1705
1811
2101
;
run;
proc print data=in;
    var date;
Run;
Occasional Contributor
Posts: 19

Re: How to read the Date in yymm format

Try this:

 

data have;

input trans_dt $;

datalines;

1705

1811

2101

;

 

data want(rename=(new_date=trans_dt));

set have;

new_date=cats('01',"-",substr(trans_dt,3),"-",2000+substr(trans_dt,1,2));

drop trans_dt;

run;

Super User
Posts: 17,907

Re: How to read the Date in yymm format

Is it already in a SAS dataset or are you reading from a text file? Is it a character or numeric variable?

 

You could use MDY() function along with SUBSTRN. This is assuming a numeric variable already in SAS.

 

my_date = MDY(substrn(old_date, 3, 2), 1, substrn(old_date, 1, 2));

 

Super User
Posts: 10,538

Re: How to read the Date in yymm format

Another

options yearcutoff=1930;
data example;
   informat trans_dt yymmn4.;
   input trans_dt;
   format trans_dt mmddyyd10.;
datalines;
1705
1811
2101
;
run;

Note teh YYMMN format.

 

Also how do you know that 2101 is in year 2021? Especially since it is in the future. The yearcutoff option sets a limit for 2 digit years and how SAS treats them by default.

 

Very strongly suggest use of a DATE value with the correct format as it is much more flexible.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 294 views
  • 0 likes
  • 7 in conversation