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

Hi,

 

I was hoping someone here could help me figure out the solution to convert number to date.

 

I have a variable called M0906_DC_TR_DTH_DT with length of 4, format as YYMMDDN8. in the original date set. It was shown as 20130513 for example. 

 

I have a trouble converting it into date. 

 

I tried to use the following code but it didn't work.

 DSCHRG_DT=input(put(M0906_DC_TR_DTH_DT, 8.), Yymmdd10.);   
	format DSCHRG_DT YYMMDDN8.;   

I also tried to change the length of M0906_DC_TR_DTH_DT to 8 instead of 4. But it didn't work neither. 

The note showed 'Invalid argument to function INPUT ......'

Any help with this regard will be greatly appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Crystal_F wrote:

Hi,

 

I was hoping someone here could help me figure out the solution to convert number to date.

 

I have a variable called M0906_DC_TR_DTH_DT with length of 4, format as YYMMDDN8. in the original date set. It was shown as 20130513 for example. 

 

I have a trouble converting it into date. 

 

I tried to use the following code but it didn't work.

 DSCHRG_DT=input(put(M0906_DC_TR_DTH_DT, 8.), Yymmdd10.);   
	format DSCHRG_DT YYMMDDN8.;   

I also tried to change the length of M0906_DC_TR_DTH_DT to 8 instead of 4. But it didn't work neither. 

The note showed 'Invalid argument to function INPUT ......'

Any help with this regard will be greatly appreciated!

 


If your variable shows that it has an existing format of YYMMDDN8. it is already a SAS date value.

What do you mean by "show as a date"? Likely all you need to do is change the display format.

You cannot change the length of a variable once created, and likely not needed.

You should show desired result of what you attempt along with code.

Here is an example of creating a variable of length 4 (number of bytes used to store a numeric, not related to display in any way) as a date value with a default format as you show and using a different format to display the value.

data junk;
  length x 4.;
  x = today();
  format x yymmddn8.;
run;

proc print data=junk;
   title "Default format";
run;

proc print data=junk;
   title "Different format applied";
   format x worddate.;
run;title;

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

If the length of the variable is 4, i suspect the date is truncated to only 4 characters. It should have length of 8.

 

DSCHRG_DT=input('20130513', yymmdd10.);   
Thanks,
Jag
ballardw
Super User

@Jagadishkatam wrote:

If the length of the variable is 4, i suspect the date is truncated to only 4 characters. It should have length of 8.

 

DSCHRG_DT=input('20130513', yymmdd10.);   

FWIW, the largest value for length 4 is 2097152. Which "restricts" dates to a maximum of 23Oct7701.

 

ballardw
Super User

@Crystal_F wrote:

Hi,

 

I was hoping someone here could help me figure out the solution to convert number to date.

 

I have a variable called M0906_DC_TR_DTH_DT with length of 4, format as YYMMDDN8. in the original date set. It was shown as 20130513 for example. 

 

I have a trouble converting it into date. 

 

I tried to use the following code but it didn't work.

 DSCHRG_DT=input(put(M0906_DC_TR_DTH_DT, 8.), Yymmdd10.);   
	format DSCHRG_DT YYMMDDN8.;   

I also tried to change the length of M0906_DC_TR_DTH_DT to 8 instead of 4. But it didn't work neither. 

The note showed 'Invalid argument to function INPUT ......'

Any help with this regard will be greatly appreciated!

 


If your variable shows that it has an existing format of YYMMDDN8. it is already a SAS date value.

What do you mean by "show as a date"? Likely all you need to do is change the display format.

You cannot change the length of a variable once created, and likely not needed.

You should show desired result of what you attempt along with code.

Here is an example of creating a variable of length 4 (number of bytes used to store a numeric, not related to display in any way) as a date value with a default format as you show and using a different format to display the value.

data junk;
  length x 4.;
  x = today();
  format x yymmddn8.;
run;

proc print data=junk;
   title "Default format";
run;

proc print data=junk;
   title "Different format applied";
   format x worddate.;
run;title;

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

Crystal_F
Quartz | Level 8
Boy! I made a very stupid mistake. Thanks for pointing out the problem. I don't need to do any conversion actually.
Kurt_Bremser
Super User

Your variable is a date. All you need to do is change the display format to your preferences.

A length of 4 gives sufficient numeric precision for dates.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1955 views
  • 2 likes
  • 4 in conversation