BookmarkSubscribeRSS Feed
bc123456
Calcite | Level 5

Hi All,

 

I am currently working with a dataset that has deliverydate field as CHAR 10. I'd like to keep it as a character value, and was able to pull the month out of the string using the following code:

 

DeliveryMonth= MONTH(input('DeliveryDate'n,MMDDYY10.));

 

DeliveryMonth is the column I'm creating, however the output is the # for the associated month. Example: 06/01/2020 in DeliveryDate is populating "6" in DeliveryMonth. Is there a way to have DeliveryMonth as "MMM" within this data step?

 

Any and all advice would be greatly appreciated.

 

Thanks,

 

Blake

8 REPLIES 8
Reeza
Super User

deliveryMonth = put(input(deliveryDate, mmddyy10.), monname3.);
bc123456
Calcite | Level 5
When I try this, I end up with a single dot in each row.
Reeza
Super User

If your problem is as described, then this does work. 

 

Assumptions:

  • Variable name is DeliveryDate
  • Variable is character
  • Variable is in the format mmddyy consistently.

You can verify your assumptions by running a proc contents on your data set as well as a PROC FREQ on your delivery date column.

 

delete_monthname.JPG

 

Astounding
PROC Star

This would happen if, in your many experiments, you already created a variable named DeliveryMonth as numeric.  You would have to get rid of it somehow, possibly:

set have (drop=DeliveryMonth);
Reeza
Super User
The log would have a message about mismatched types wouldn't it?
Astounding
PROC Star

There would be some messages ... character to numeric conversion, invalid data.

ballardw
Super User

@bc123456 wrote:
When I try this, I end up with a single dot in each row.

Show your entire data step code with notes from the log. Copy the text from the log including any messages. Paste on the forum into a code box opened using the the </> icon to preserve formatting.

 

If you use code structure like:

Data test;
   set test;
   <testing code>
run;

Then in one or more previous iterations of that data step you created DeliveryMonth as numeric and SAS won't let you put character values into numeric values. The above Data test; set test; is a very poor idea for new SAS coders to use because you completely rewrite your data every time. So logic errors can remove records or lose variables you did not intend. And a host of more subtle bad things can happen if you recode variable values back to the same variable.

 

And if the you had initially created a date variable for keeping in your data you would likely almost certainly not need an additional variable but use the MONNAME format with a data value as needed in analysis, reports or graphs. One reason not to use character Month values is that they will not sort properly in reports or graphs. The year would start with April, then August and ends with November.

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!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 3523 views
  • 2 likes
  • 5 in conversation