DATA Step, Macro, Functions and more

calculate month from date

Posts: 0

calculate month from date

I've x=20090801 ( as $8 char)

I want to calculate y = month(x), so that y = 08.

But if i use it like this it gives me . (missing values).

Please sugest.
Super Contributor
Super Contributor
Posts: 3,174

Re: calculate month from date

Recommended SAS DOC reading on Dates and using INPUT function to convert from characters and using MONTH function. Google advanced search argument:

using sas date variables

Scott Barry
SBBWorks, Inc.
Posts: 36

Re: calculate month from date

You have x as a character string for date with a yymmddn8. format.
The simplest method is to use a simple substring:
month = substr(x,5,2);
This create month as a string. If you want month as a numeric:
month = input(substr(x,5,2),2.);

If you want to use the month function you need to first convert the string to a SAS date.
date = input(x,yymmdd8.);
y = month(date);
Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation