I have a variable in the form of yymmdd, and I want to extract yymm from it without changing the date format. I konw year() and month(),but I don't know how to get year and month in one variable. I asked the question just now and got the answer of using put to resolve it, but after use the method, i found the format is character. Here is my code. thanks a lot.
data have;
format dt yymmdd10.;
input dt yymmdd10.;
datalines;
2016-05-21
2017-08-12
2014-03-02
;
run;
data want;
set have;
yy = put(dt,yymmn6.);
run;
@lixuan wrote:
Thanks, if I wnna get a new variable of yymm in date format, how can I do?
You could copy the date into another variable and attach a different format.
newdate= olddate ;
format olddate yymmdd10. newdate yymmn6. ;
Otherwise you will need to convert to something other than a date, since an actual date value requires a day of the month.
newdate_ch = put(olddate,yymmn6.);
newdate_num = input(put(olddate,yymmn6.),6.);
* or ;
newdate_num = year(olddate)*100 + month(olddate);
Apply the FORMAT don't convert it.
format dt yymmn6.;
Thanks, if I wnna get a new variable of yymm in date format, how can I do?
Just input the result of your put. e.g.:
data want; set have; yy = input(put(dt,yymmn6.),8.); run;
Art, CEO, AnalystFinder.com
Ok, Thank you . Combining with both your solvements, I get the answer, however I wonder if there is a simpler way ?
data want;
set have;
yy = input(put(dt,yymmn6.),8.);
format yy yymmn6.;
run;
@lixuan wrote:
Ok, Thank you . Combining with both your solvements, I get the answer, however I wonder if there is a simpler way ?
data want; set have; yy = input(put(dt,yymmn6.),8.); format yy yymmn6.; run;
You cannot apply the YYMMN format to a number like 201,707. SAS stores dates as the number of days since start of 1960 so something like 201,707 would be in the year 2512, not in the year 2017.
Yes, I check again and find that i am wrong. Thank you .
Another option is to duplicate the variable and then apply the format to the new variable. You can also apply formats in specific locations to override whats in your data. For example, if you want to create a report thats at the monthly level but only have data, you can apply the format in PROC REPORT or MEANS/TABULATE and it will aggregate correctly.
@lixuan wrote:
Thanks, if I wnna get a new variable of yymm in date format, how can I do?
You could copy the date into another variable and attach a different format.
newdate= olddate ;
format olddate yymmdd10. newdate yymmn6. ;
Otherwise you will need to convert to something other than a date, since an actual date value requires a day of the month.
newdate_ch = put(olddate,yymmn6.);
newdate_num = input(put(olddate,yymmn6.),6.);
* or ;
newdate_num = year(olddate)*100 + month(olddate);
OK, I get it. Thank you all very much.
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.