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
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.
got solution from ChatGPT
data example; input date :date9.; year_month = put(date, yymmn6.); format date date9.; format year_month yymmn6.; cards; 20AUG2017 ; run;
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.
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).
Thank you noted
@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
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.
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).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.