data month;
set work.student_roster;
length justmonth $15;
justmonth = scan(birth_day, 2, 3);
run;
I need to extract the month from a field formatted as a date9. For example, if the value is 31Jan1999, I need to extract "Jan" as a new variable. I'm using the scan function but I'm getting weird results:
I think this is because of the format. I need to reformat this variable into a string? If that's correct, can you please suggest the best way to go about it?
@ChrisBrooks wrote:
Hi @AMMAN
Actually the dates in your attachment aren't formatted as date9 but as mmddyy9 but the same solution should work either way. You can use a put statement with the monnmae format as shown below (I've used the first three records from your attachment
Why not
justmonth= put(bday, monname3.);
If you have a SAS date, which is a variable that's numeric with a date format then you have a number and SCAN() will not work on a number. You can convert it to a character or you can use formats to get your desired output.
data have;
format date date9.;
do date='01Jan2018'd to '31Dec2018'd by 30;
month = put(date, monname3.);
day = day(date);
output;
end;
run;
@AMMAN wrote:
data month; set work.student_roster; length justmonth $15; justmonth = scan(birth_day, 2, 3); run;
I need to extract the month from a field formatted as a date9. For example, if the value is 31Jan1999, I need to extract "Jan" as a new variable. I'm using the scan function but I'm getting weird results:
I think this is because of the format. I need to reformat this variable into a string? If that's correct, can you please suggest the best way to go about it?
Thank you!
Hi @AMMAN
Actually the dates in your attachment aren't formatted as date9 but as mmddyy9 but the same solution should work either way. You can use a put statement with the monnmae format as shown below (I've used the first three records from your attachment
data have;
infile datalines dlm=",";
length name $5 bday 8;
informat bday mmddyy9.;
format bday mmddyy9.;
input name bday;
datalines;
Chas,5/15/1986
Pearl,3/6/1997
Troy,3/26/1999
;
run;
data want;
set have;
justmonth=substr(strip(put(bday, monname.)),1,3);
run;
@ChrisBrooks wrote:
Hi @AMMAN
Actually the dates in your attachment aren't formatted as date9 but as mmddyy9 but the same solution should work either way. You can use a put statement with the monnmae format as shown below (I've used the first three records from your attachment
Why not
justmonth= put(bday, monname3.);
Good point @ballardw that's more succinct!
Thank you! This worked!
Thank you. This worked. In fact, I tried all the recommendations and they worked! I'm not sure how to mark "solution" in this case.
Good to hear @AMMAN I'd encourage you to pick and solution and mark it as accepted. It helps others when searching/filtering through posts.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.