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

Hi, I need help in extracting year and month from date9. date from the following sample data

 

data work.dates;
	format my_date date9.;
	input my_date date9.;
	datalines;
08JAN2019
26JUN2020
05DEC2021
;
run;

 

Data Want

New_date

201901

202006

202112

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

The PUT not needed.

 

data example;
   input date :date9.;
   format date yymmn6.;
   cards;
   20AUG2017
   ;

 

 

All you have to do is change the format on DATE. No need to create a new variable.

 

--
Paige Miller

View solution in original post

8 REPLIES 8
Solly7
Pyrite | Level 9

got solution from ChatGPT

data example;
   input date :date9.;
   year_month = put(date, yymmn6.);
   format date date9.;
   format year_month yymmn6.;
   cards;
   20AUG2017
   ;
run;
PaigeMiller
Diamond | Level 26

The PUT not needed.

 

data example;
   input date :date9.;
   format date yymmn6.;
   cards;
   20AUG2017
   ;

 

 

All you have to do is change the format on DATE. No need to create a new variable.

 

--
Paige Miller
Solly7
Pyrite | Level 9
Thank you so much
PaigeMiller
Diamond | Level 26

I add that ChatGPT did not give you the best answer, it did not give you the easiest answer, and it gave you an answer that will cause difficulties if you need to perform arithmetic or logical operations on these month values (because these month values are now character, and you can't do math easily on character strings).

--
Paige Miller
Solly7
Pyrite | Level 9

Thank you noted

FreelanceReinh
Jade | Level 19

@PaigeMiller wrote:

I add that ChatGPT did not give you the best answer, it did not give you the easiest answer, and it gave you an answer that will cause difficulties if you need to perform arithmetic or logical operations on these month values (because these month values are now character, and you can't do math easily on character strings).


Not to mention the incorrect FORMAT statement it suggested, attempting to apply a numeric format to a character variable:

11    data example;
12       input date :date9.;
13       year_month = put(date, yymmn6.);
14       format date date9.;
15       format year_month yymmn6.;
                           -------
                           48
ERROR 48-59: The format $YYMMN was not found or could not be loaded.

16       cards;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.EXAMPLE may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds
ChrisHemedinger
Community Manager

I unmarked the ChatGPT answer as solution and instead selected the one from @PaigeMiller . 

 

ChatGPT is an amazing tool...but when it comes to answering precise questions, it will simply make stuff up. I don't mind people trying it and sharing their experience on the community, but let's not help train the model with wrong/suboptimal answers.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
PaigeMiller
Diamond | Level 26

@ChrisHemedinger 

 

I was thinking the same about ChatGPT learning from wrong or sub-optimal answers. In addition, there are a lot of SAS users who think that if they want to change a format, a new variable is needed! I see this all the time in the people I work with, and with data sets of potentially millions of records and possibly hundreds of variables, creating one new variable is a lot of work for the computer and a lot more disk space, not to mention the problems caused by converting a numeric date value into a character string (as ChatGPT is recommending and which I objected to).

--
Paige Miller

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
  • 8 replies
  • 1726 views
  • 8 likes
  • 4 in conversation