- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Say we have a time variable, in date9, or datetime18. format. I would like to grab only the month and year part of the value and save it. For example, turn 31Dec2015 to Dec2015. datepart() function grabs the specific date of a time value, is there something similar to that but only grabs the month and year? Thanks.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Even though the underlying value changes day by day, most SAS procedures that summarize will respect the format and group by the formatted value instead of the underlying value.
To take things step by step and guarantee the result you want, create a new variable with the month and year. After applying DATEPART once:
category = put(date_only, monyy7.);
Unfortunately, the values you get will not necessarily be in the order that you want. You might want to try a format that contains the year followed by the month, like 1960-01 to keep the values in order.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SAS has functions for both year and month if you pass it a numeric date.
year(date) or
month(date)
These will be numeric results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you. They grab only the year or the month of a time value respectively, and save it as regular numeric format, not date format. Is there another function that grabs both the year and month of a certain time value, and preferably save it as a date format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You already know how to apply the DATEPART function:
d = datepart(dt_var);
Try applying the right format to the result:
format d monyy7.;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
With the DT version of that format no need for new variable.
2579 data _null_;
2580 x = datetime();
2581 put x=dtmonyy7.;
2582 run;
x=JAN2016
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data test;
set test;
put date=MONYY7.;
run;
However, the "date" variable has not changed from before, still shows the entire date and seems to be date9. format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Because you have a datetime variable use dtFORMAT name instead of just the format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I tried it. I comes out to be always JAN1960, double click on it shows "1/1/1960". What may be causing this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SAS Date values are the number of days since 1/1/1960. If your "date" values show as 1/1/1960 then the way you created them was incorrect as they are resolving to something between 0 and 2. Print the data or change the display with a format like best10. and see what the values are then.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Without looking over your shoulder and seeing the steps you applied, we're guessing.
Here's my guess.
You applied the datepart function an extra time. It was already applied once, converting the datetime variable to a date. Then you applied it a second time, converting the date to January 1, 1960.
Try printing the variable without a format and see what the actual value is.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
i have tried keeping the raw date_time variable, and making 2 variables, one with date9, format, the other with monyy7. format, both using datepart(date_time) function, but they come out the same, both still containing the day of the month. so how would i fix this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
While a number of the suggested approaches would work, I'll repeat my original idea.
After applying DATEPART only once, print the result using the MONYY7 format.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That depends on the proc, PROC SQL will distinguish between the days, but PROC MEANS/FREQ will respect the format.
Also, your question initially stated you wanted to create a date variable. A date variable requires a day.
I see a few options:
1. Convert all to character fields - dates will not sort properly in your tables (APR is first month to show)
2. Change all dates to either 1st or 15th of the month so they'll aggregate together.
3. Use procs above, which respect date format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Even though the underlying value changes day by day, most SAS procedures that summarize will respect the format and group by the formatted value instead of the underlying value.
To take things step by step and guarantee the result you want, create a new variable with the month and year. After applying DATEPART once:
category = put(date_only, monyy7.);
Unfortunately, the values you get will not necessarily be in the order that you want. You might want to try a format that contains the year followed by the month, like 1960-01 to keep the values in order.