Help using Base SAS procedures

Trimming dates to yyyy only

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Trimming dates to yyyy only

How can I reformat a date from mmmyy7. to yyyy only? The data set I’m working in has multiple years of data. I want to strip/trim the ddMMM from the YYYY, i.e. leaving on the YYYY.

Message was edited by: Woody Sutton


Accepted Solutions
Solution
‎06-16-2014 10:49 AM
Super User
Posts: 10,538

Re: Trimming dates to yyyy only

Actually with SAS dates I wouldn't change any values unless you want to add a new variable to a data set. Most analysis functions will use a formatted value and in your case that format would be YEAR4. when you need it. If you modify the date value I would bet that in short order someone will want quarterly or monthly summaries. Which with the original date value means you likely only have to change ther format to one appropriate. You don't even have to make the format change permanent, use at run time. However Proc Datasets will let you change change the default format by code or open the dataset in the SAS explorer in column view, click on the variable and type the desired format in the format box.

To build a new variable: Year=year(datevalue);

View solution in original post


All Replies
Super User
Posts: 9,687

Re: Trimming dates to yyyy only

data x;
 x='01jan2012'd;
 format x year4.;
 run;


Xia Keshan

Super User
Super User
Posts: 7,413

Re: Trimming dates to yyyy only

Hi,

Or:

data x;

     x='01jan2012'd;

     y=year(x);

run;

Solution
‎06-16-2014 10:49 AM
Super User
Posts: 10,538

Re: Trimming dates to yyyy only

Actually with SAS dates I wouldn't change any values unless you want to add a new variable to a data set. Most analysis functions will use a formatted value and in your case that format would be YEAR4. when you need it. If you modify the date value I would bet that in short order someone will want quarterly or monthly summaries. Which with the original date value means you likely only have to change ther format to one appropriate. You don't even have to make the format change permanent, use at run time. However Proc Datasets will let you change change the default format by code or open the dataset in the SAS explorer in column view, click on the variable and type the desired format in the format box.

To build a new variable: Year=year(datevalue);

New Contributor
Posts: 2

Re: Trimming dates to yyyy only

thank you all for your help. i was successful in formatting the data.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 300 views
  • 6 likes
  • 4 in conversation