- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Apply the FORMAT don't convert it.
format dt yymmn6.;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, if I wnna get a new variable of yymm in date format, how can I do?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Just input the result of your put. e.g.:
data want; set have; yy = input(put(dt,yymmn6.),8.); run;
Art, CEO, AnalystFinder.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I check again and find that i am wrong. Thank you .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
OK, I get it. Thank you all very much.