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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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.

TanviG
Fluorite | Level 6

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

ballardw
Super User

@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.

Tom
Super User Tom
Super User

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

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 66721 views
  • 4 likes
  • 3 in conversation