Help using Base SAS procedures

How do I extract the month and year from a date?

Reply
N/A
Posts: 1

How do I extract the month and year from a date?

Hello Friends,

I am trying to get my mind around this issue, and I need your advice regarding it.

I have a variable "Date" in my sas dataset in the format "01DEC05".

I want to extract the month "DEC", and the year "05" using the DATA step.

So far I tried using the SUBSTR(Right =) function; it does work for the char variable but it does not work for the numeric variable.

Here's my code:

data work.stocks;

set sashelp.stocks;

Inital=substr(Stock,1,1);

Month=SUBSTR(Date,3,3);

Year=SUBSTR(Date,6,2);

put @1 Month @5 Year;

run;

This being my first post here, my apologies if the code tags are not inserted properly.

Best Regards

asar_k

Ref:SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Super User
Posts: 9,691

Re: How do I extract the month and year from a date?

data _null_;

d='01mar2015'd;

m=put(d,monname3.);

y=put(d,year4.);

put m= y=;

run;

Ask a Question
Discussion stats
  • 1 reply
  • 1086 views
  • 0 likes
  • 2 in conversation