BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AMMAN
Obsidian | Level 7
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:

 

weird.JPG

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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

 


@ChrisBrooks

 

Why not

justmonth= put(bday, monname3.);

View solution in original post

8 REPLIES 8
Reeza
Super User

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:

 

weird.JPG

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? 


 

AMMAN
Obsidian | Level 7

Thank you!

ChrisBrooks
Ammonite | Level 13

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;
ballardw
Super User

@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

 


@ChrisBrooks

 

Why not

justmonth= put(bday, monname3.);

ChrisBrooks
Ammonite | Level 13

Good point @ballardw that's more succinct!

AMMAN
Obsidian | Level 7

Thank you!  This worked!

AMMAN
Obsidian | Level 7

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.

ChrisBrooks
Ammonite | Level 13

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 10104 views
  • 1 like
  • 4 in conversation