DATA Step, Macro, Functions and more

calculate month from date

Reply
N/A
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

Posted in reply to deleted_user
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 site:sas.com

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 36

Re: calculate month from date

Posted in reply to deleted_user
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
  • 113 views
  • 0 likes
  • 3 in conversation