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

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.

SAS.png

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
PS1214
Calcite | Level 5

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;

 

 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
PS1214
Calcite | Level 5

Are you talking about line 6? If so, it does not return back any value.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
PS1214
Calcite | Level 5

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.

PaigeMiller
Diamond | Level 26

Okay, let's eliminate obvious potential problems here.

 

  1. After you changed the code, did you actually execute the code with the changes?
  2. Are there errors or warnings in the log?
--
Paige Miller
PS1214
Calcite | Level 5

This is what the log shows

sas2.png

 

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;
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
PS1214
Calcite | Level 5
Ok thank you for your patience and help.

I was hoping there was a way.
PS1214
Calcite | Level 5
The code you posted does not seem to work.

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
  • 11 replies
  • 3532 views
  • 1 like
  • 2 in conversation