BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Riteshdell
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

6 REPLIES 6
Shmuel
Garnet | Level 18

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;

art297
Opal | Level 21

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

PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
PBsas
Obsidian | Level 7

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;

Reeza
Super User

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));

 

ballardw
Super User

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.

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
  • 6 replies
  • 7310 views
  • 0 likes
  • 7 in conversation