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

Hi, I have a date variable in my dataset, a, that has a format yyyymmdd10. I want to convert it to a vairable with format yyymm6.

 

For example, 

record      a                      want

1             2003-05-21      200305

 

 

However, I wrote the following SAS code and it gave me something else:

data output;
	set input;
	want = datepart(a);
	format want yymmn6.;
run;

This is what I get now:

record     a                       want

1             2003-05-21      196001

 

Does anyone know how I can fix it? Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You don't really need a new variable, you can just change the format on variable A to yymmn6.;

 

data output;
	set input;
	format a yymmn6.;
run;

Also, DATEPART makes no sense here, and is not needed, as your value is already a valid SAS date.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You don't really need a new variable, you can just change the format on variable A to yymmn6.;

 

data output;
	set input;
	format a yymmn6.;
run;

Also, DATEPART makes no sense here, and is not needed, as your value is already a valid SAS date.

--
Paige Miller
maguiremq
SAS Super FREQ

If I understand correctly, this is what you want:

 

data have;
input record a :yymmdd10.;
format a yymmdd10.;
datalines;
1 2003-05-21
;
run;

data want;
	set have;
		want = put(a, yymmn6.);
run;
Reeza
Super User

You want a three digit year? That isn't a standard format that I'm aware of...if you can use a 4 digit year then it's quite simple to use a new format. If you really want only the last three digits you'll need a custom format or a manual calculation. 

 

Assuming you want the YYYYMM that would be YYMMx./YYMMD6. format - D is for the dash.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=leforinforref&docsetTarge...


DATEPART takes the date portion of a datetime, I cannot say if that is necessary as you don't indicate types. Try the YYMMD6. format 

 


@newboy1218 wrote:

Hi, I have a date variable in my dataset, a, that has a format yyyymmdd10. I want to convert it to a vairable with format yyymm6.

 

For example, 

record      a                      want

1             2003-05-21      200305

 

 

However, I wrote the following SAS code and it gave me something else:

data output;
	set input;
	want = datepart(a);
	format want yymmn6.;
run;

This is what I get now:

record     a                       want

1             2003-05-21      196001

 

Does anyone know how I can fix it? Thanks!

 


 

ballardw
Super User

@newboy1218 wrote:

Hi, I have a date variable in my dataset, a, that has a format yyyymmdd10. I want to convert it to a vairable with format yyymm6.

 

For example, 

record      a                      want

1             2003-05-21      200305

 

 

However, I wrote the following SAS code and it gave me something else:

data output;
	set input;
	want = datepart(a);
	format want yymmn6.;
run;

This is what I get now:

record     a                       want

1             2003-05-21      196001

 

Does anyone know how I can fix it? Thanks!

 


Do not use the DATEPART function with values that are already dates. DATEPART is designed to extract the date, number of days, from a DATETIME value, which uses seconds. When you used the datepart function SAS assumed that the "date" was a date time and the underlying datetime represented by the number of days when considered as seconds means the "date" was treated as 01JAN60:04:24:06.

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

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
  • 4 replies
  • 1765 views
  • 2 likes
  • 5 in conversation