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

Hi,

 

In my file I have date written in this form (20131126). I want to keep only year and month(201311)

I tried the following 

 

Data want;

    set have;

    Format date yymmn;  (not my variable name is date)

run;

 

This code is not working. I am not getting the desired result. Please help.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@abdulla wrote:
No, it doesn't work. I got 2.01E7. Also I am continuously getting the following msg.
ERROR: There was a problem with the format so BEST. was used.

If your value is printing as 2.01E7 then it is neither a date (number of days since 1960) or a datetime (number of seconds since 1960).

269   data test;
270     x= 2.01E7 ;
271     put 'COMMA15. ' @13 x comma15. ;
272     put 'DATE9. '   @13 x date9. ;
273     put 'DATETIME20. ' @13 x datetime20. ;
274   run;

COMMA15.         20,100,000
DATE9.      *********
DATETIME20.   20AUG1960:15:20:00

Looks like you might have stored you dates as numbers where the tens and ones place represent the day of the month, etc.

To convert them into actual dates use could use an INPUT() function.  You will first need to convert them into a string.

data want ;
  set have;
  date = input(put(date,8.),yymmdd8.);
  format date yymmdd10. ;
run;

 

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

Try  this:

format date yymmn6.;
abdulla
Pyrite | Level 9
No, it doesn't work. I got 2.01E7. Also I am continuously getting the following msg.
ERROR: There was a problem with the format so BEST. was used.
SASKiwi
PROC Star

OK, that means your date isn't an actual SAS date, which holds a number for the days since 1 Jan 1960.

 

Run this test with an actual SAS date and check the log to prove it for yourself:

data test;
   mydate='14may2018'd;
   format mydate yymmn6.;
   put _all_;
run;

Please post an example of your date data.

 

Tom
Super User Tom
Super User

@abdulla wrote:
No, it doesn't work. I got 2.01E7. Also I am continuously getting the following msg.
ERROR: There was a problem with the format so BEST. was used.

If your value is printing as 2.01E7 then it is neither a date (number of days since 1960) or a datetime (number of seconds since 1960).

269   data test;
270     x= 2.01E7 ;
271     put 'COMMA15. ' @13 x comma15. ;
272     put 'DATE9. '   @13 x date9. ;
273     put 'DATETIME20. ' @13 x datetime20. ;
274   run;

COMMA15.         20,100,000
DATE9.      *********
DATETIME20.   20AUG1960:15:20:00

Looks like you might have stored you dates as numbers where the tens and ones place represent the day of the month, etc.

To convert them into actual dates use could use an INPUT() function.  You will first need to convert them into a string.

data want ;
  set have;
  date = input(put(date,8.),yymmdd8.);
  format date yymmdd10. ;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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