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

Hi everyone

 

I have a column in my data set that is recognized by SAS as numeric and where the values are in the format of YYYY-MM-DD. I would like to convert it into YYYYMMDD format.

 

To do so I took the variable with the current format YYMMDD10 and create a new variable "date2" which is already numeric and in the format that I want it to be, YYMMDDN8. However, when I run the code, the "date2" stays empty.

 

Can you tell me how I can solve this? 

I want to either a) transform the current date variable to YYMMDDN8 or b) create a new variable with YYMMDDN8 format.

 

Here is the code I used:

 

data xyz;
set xy;
date2= input(date, yymmdd10.);
format date2 YYMMDDN8.;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS has two types of variables floating point numbers and fixed length character strings.  If you variable has the YYMMDD10. format specification attached to it and displays as the 10 character string with hyphens, for example "2021-04-10", then it is a numeric variable and the value stored is the number of days since 1960.   If you have another numeric variable that has the YYMMDDN8. format attached to it then the same number would be displayed as the 8 character string "20210410".  So to make the new variable have the same date as the old one just assign the value without modification.

date2=date;
format date2 yymmddn8.;

The INPUT() function is for using an informat to convert text into values.  If you call it with a numeric variable instead of a character variable then SAS will auto convert the number into a string for the INPUT() function to operate one.  It will do this using the BEST12. format, not any format that might or might not be attached to the variable.  So a date value of '10APR2021'd (which is the number 22,380) would become string "       22380"  (notice the 7 leading spaces).  The first 10 characters of that string cannot be interpreted by the YYMMDD10 informat as a valid date.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

SAS has two types of variables floating point numbers and fixed length character strings.  If you variable has the YYMMDD10. format specification attached to it and displays as the 10 character string with hyphens, for example "2021-04-10", then it is a numeric variable and the value stored is the number of days since 1960.   If you have another numeric variable that has the YYMMDDN8. format attached to it then the same number would be displayed as the 8 character string "20210410".  So to make the new variable have the same date as the old one just assign the value without modification.

date2=date;
format date2 yymmddn8.;

The INPUT() function is for using an informat to convert text into values.  If you call it with a numeric variable instead of a character variable then SAS will auto convert the number into a string for the INPUT() function to operate one.  It will do this using the BEST12. format, not any format that might or might not be attached to the variable.  So a date value of '10APR2021'd (which is the number 22,380) would become string "       22380"  (notice the 7 leading spaces).  The first 10 characters of that string cannot be interpreted by the YYMMDD10 informat as a valid date.

sasmme
Calcite | Level 5

Amazing! Thank you so much!!! Problem solved.

PaigeMiller
Diamond | Level 26

@sasmme wrote:

Hi everyone

 

I have a column in my data set that is recognized by SAS as numeric and where the values are in the format of YYYY-MM-DD. I would like to convert it into YYYYMMDD format.

 

To do so I took the variable with the current format YYMMDD10 and create a new variable "date2" which is already numeric and in the format that I want it to be, YYMMDDN8. However, when I run the code, the "date2" stays empty.

 

Can you tell me how I can solve this? 

I want to either a) transform the current date variable to YYMMDDN8 or b) create a new variable with YYMMDDN8 format.

 

Here is the code I used:

 

data xyz;
set xy;
date2= input(date, yymmdd10.);
format date2 YYMMDDN8.;
run;

 

 


No need to create a new variable; just change the format on date.

 

data xyz;
set xy;
format date YYMMDDN8.;
run;

or better yet if you have large data sets

 

proc datasets library=work;
modify xy;
format date yymmddn8.;
run;
--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 4500 views
  • 0 likes
  • 3 in conversation