DATA Step, Macro, Functions and more

Convert Date to Character

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Convert Date to Character

I have numeric date as 201711 and I want to convert it in 30Nov2017 in character format. Anyone help please.


Accepted Solutions
Solution
a week ago
Super User
Posts: 13,292

Re: Convert Date to Character

Here's an example creating a date valued variable and a character version.

data example;
   x=201711;
   date = intnx('month',input(put(x,6. -L),yymmn6.),0,'E');
   format date date9.;
   chardate = put(date,date9.);
run;

 

 

For almost any purpose I would suggest creating a date valued version as you can sort the values correctly, get parts of variable as needed, change grouping in analysis by changing the display format and others.

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 13,292

Re: Convert Date to Character

Here's an example creating a date valued variable and a character version.

data example;
   x=201711;
   date = intnx('month',input(put(x,6. -L),yymmn6.),0,'E');
   format date date9.;
   chardate = put(date,date9.);
run;

 

 

For almost any purpose I would suggest creating a date valued version as you can sort the values correctly, get parts of variable as needed, change grouping in analysis by changing the display format and others.

Occasional Contributor
Posts: 9

Re: Convert Date to Character

can you please explain a bit how you converted numeric value in a sas date ? Many Thanks.

Super User
Posts: 13,292

Re: Convert Date to Character

[ Edited ]

@TanviG wrote:

can you please explain a bit how you converted numeric value in a sas date ? Many Thanks.


SAS provides a number of informats for reading character data into SAS date values. The informat yymmn6. reads a character value with the first four digits as a year and the next two digits as months. The  "n" says there isn't a separator as might occur in some values like 2018/05. Since informats read character values we turn the numeric value into a character using PUT with an appropriate format. The -L in the Put is to left align the value in case you have any single digit month values such as 20177 as the informat expects the first 4 characters to be a year and with out the -L you could get a leading space. The values read with YYMMN6. format will assume the day of the month is 1. The intnx function advances the date to the end of the month (interval of 'MONTH', interval count of 0 is same month and the alignment of 'E' is end).

 

The date could also be built using the MDY function after determining the Year and Month parts but for simplicity of code you would still want to assume the day of month 1 and use the intnx function to shift.

Super User
Super User
Posts: 7,931

Re: Convert Date to Character

[ Edited ]

@TanviG wrote:

I have numeric date as 201711 and I want to convert it in 30Nov2017 in character format. Anyone help please.


You currently do NOT have a date. If you have a numeric variable with the value 201,711 it is NOT a date. Dates are the number of days since 1/1/1960. 

If you want to interpret it as date then use a PUT() statement to convert it the a character string and use INPUT() statement to convert it to a date. 

 

If you want to store the date value you have created into a character variable then use a PUT() statement. For that style use the DATE9. format.

data _null_;
  number = 201711;
  date1=input(put(number,z6.)||'01',yymmdd8.) ;
  date2 = intnx('month',date1,0,'e');
  char_date = put(date2,date9.);
  put (_numeric_) (= best12. /);
  put /(_numeric_) (= comma12. /);
  put /(date:) (= yymmdd10./);
  put /char_date = ;
run;
number=201711
date1=21124
date2=21153

number=201,711
date1=21,124
date2=21,153

date1=2017-11-01
date2=2017-11-30

char_date=30NOV2017

You can also use some arithmetic to pull the YEAR and MONTH from your number and pass them to the MDY() function.

  date1 = mdy(mod(number,100),1,int(number/100));

 

☑ This topic is solved.

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

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