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