@marleeakerson wrote:
I am trying to extract the year from a date variable (in the dataset as MM/DD/YYYY). But every time I use the substr statement, I get random numbers that are not even in the date variable at all. I first converted the original date variable from the number of days passed from Jan 1,1960 format to the MM/DD/YYYY format so i am wondering if that is contributing to it.
Thank you!
data want;
set have;
Year=substr(Date,7,4);
run;
You need to show your data. If DATE is a character string like '10/03/2019' then you are asking for characters number 7 thru 10 which would be '2019'. If DATE is number with the MMDDYY10. format attach to it then it will first be converted to a character string using the BEST12. format. So '03OCT2019'd would be converted to:
" 21825"
And then the 7th to 10th characters would be " 218".
So you want a number out?
year=year(date);
Or a string?
year=put(year(date),4.);