Hello,
I am very new to SAS and I was wondering how I could change the YearMon date into another format.
I did it successfully by using the comment on line 4 by removing the "M", but I do not know how to do it without that code.
Maybe there is an informat that I am missing, as I have already tried anydtdte32 and yymm.
I am trying to do it in simple way.
Thank you for your time.
Okay, so obviously your variable YEARMON is not a valid SAS date, it most likely is a character string. This is an important distinction. In order to change this to a valid SAS date, you must use an informat that matches the format of the character string, and I am not aware of such an informat. (There is the YYMMN. informat but that does not allow the letter M in the date).
So, you will have to parse the value of character variable yearmon into numeric year and numeric month.
data datechange;
set pg1.eu_occ;
newdate = mdy(input(substr(yearmon,1,4),4.),1,input(substr(yearmon,6,2),2.);
format newdate date9.;
run;
You don't say what result you want, what format you would like to see your dates displayed as.
However, looking at your code, I think all you have to do is change the format statement to something like this:
format newdate date9.;
You can choose any valid SAS date format to obtain the result you want.
The 2017M09 comes directly from the format statement that you are using, yymm10. — and any other valid format statement will change the appearance of the date, but will not change the underlying numeric SAS date value.
In the future, please copy and paste your SAS code into the window that appears when you click on the running man icon.
Hello,
Thank you for responding. I am trying to figure out any valid date format, it doesn't matter. I was experimenting on how to change the variable YearMon to another date format, but I wasn't having any luck in doing it.
data datechange;
set pg1.eu_occ;
/* YearMon=tranwrd(YearMon, "M", "-"); */
Newdate=input(YearMon, anydtdte32.);
format Newdate yymm10.;
run;
@PS1214 wrote:
Hello,
Thank you for responding. I am trying to figure out any date format, it doesn't matter. I was experimenting on how to change the variable YearMon to another date format, but I wasn't having any luck in doing it.
I explained how to change the format in my earlier reply.
Are you talking about line 6? If so, it does not return back any value.
@PS1214 wrote:
Are you talking about line 6? If so, it does not return back any value.
I'm afraid this contains no helpful information.
Show us the code you used. Show us the result.
I am not sure what more you want.
data datechange;
set pg1.eu_occ;
/* YearMon=tranwrd(YearMon, "M", "-"); */
Newdate=input(YearMon, anydtdte32.);
format Newdate date9.;
run;
I used that, and the output is the same as in the picture in post one.
I replaced "date9" with many other formats and it does not work.
Okay, let's eliminate obvious potential problems here.
This is what the log shows
Maybe I can say it this way.
This doesn't work.
data datechange;
set pg1.eu_occ;
Newdate=input("2018M03",anydtdte32.);
format NewDate yymm10.;
run;
But this works
data datechange;
set pg1.eu_occ;
Newdate=input("2018-03",anydtdte32.);
format NewDate yymm10.;
run;
Okay, so obviously your variable YEARMON is not a valid SAS date, it most likely is a character string. This is an important distinction. In order to change this to a valid SAS date, you must use an informat that matches the format of the character string, and I am not aware of such an informat. (There is the YYMMN. informat but that does not allow the letter M in the date).
So, you will have to parse the value of character variable yearmon into numeric year and numeric month.
data datechange;
set pg1.eu_occ;
newdate = mdy(input(substr(yearmon,1,4),4.),1,input(substr(yearmon,6,2),2.);
format newdate date9.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.