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.

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
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-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!

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