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

 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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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);

 

View solution in original post

9 REPLIES 9
Reeza
Super User

 

Apply the FORMAT don't convert it.

 

format dt yymmn6.;

 

lixuan
Obsidian | Level 7

Thanks, if I wnna get a new variable of yymm in date format, how can I do?

art297
Opal | Level 21

Just input the result of your put. e.g.:

data want;
  set have;
  yy = input(put(dt,yymmn6.),8.);
run;

Art, CEO, AnalystFinder.com

 

lixuan
Obsidian | Level 7

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;
Tom
Super User Tom
Super User

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

lixuan
Obsidian | Level 7

Yes, I check again and find that i am wrong. Thank you .

Reeza
Super User

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. 

 

 

Tom
Super User Tom
Super User

@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);

 

lixuan
Obsidian | Level 7

OK, I get it. Thank you all very much.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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