BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
znhnm
Quartz | Level 8

I have a time type columnç One example record is: 03JUN22:00:00:00.

How can I get the month part of it, JUN? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

As @andreas_lds noted, this can be a datetime variable (numeric) or a string. You can use PROC CONTENTS to find out what it is.

 

In the first case, get the date part, and use a format to get the month name, e.g.:

Length Month $3;
Month=put(datepart(<column name>),monyy5.);

The MONYY format actually gets both the month and the year, e.g. "JUN22", but because we set the length of the MONTH variable to 3, it will only contain "JUN".

 

In the second case, you can use SUBSTR:

Length Month $3;
Month=substr(<column name>,3,3);

View solution in original post

7 REPLIES 7
Astounding
PROC Star
This will get you the month as a number from 1 to 12:

m = month(datepart(varname));

Will that do?

Or maybe your original variable is a character string and you could use:

m = substr(varname, 3, 3);
andreas_lds
Jade | Level 19

This seems to be a datetime variable, that is a numeric variable with a format attached displaying the value as date and time. Maybe it is a string. Impossible to know without seeing the result of proc contents of the dataset containing the variable.

znhnm
Quartz | Level 8
Hi, thank you. It's Num 8 type DATETTIME. format.
s_lassen
Meteorite | Level 14

As @andreas_lds noted, this can be a datetime variable (numeric) or a string. You can use PROC CONTENTS to find out what it is.

 

In the first case, get the date part, and use a format to get the month name, e.g.:

Length Month $3;
Month=put(datepart(<column name>),monyy5.);

The MONYY format actually gets both the month and the year, e.g. "JUN22", but because we set the length of the MONTH variable to 3, it will only contain "JUN".

 

In the second case, you can use SUBSTR:

Length Month $3;
Month=substr(<column name>,3,3);
znhnm
Quartz | Level 8
It helped a lot, thank you!
Ashish_Vashist
Fluorite | Level 6
Data Question;
	Date_Time_Variable = Dhms('03JUN22'd,00,00,00);
	format Date_Time_Variable datetime. ;
run;

/* YOU CAN USE THIS COUSTOM FORMAT FOR MONTHS*/
Proc Format;
	value  MonthName    1 = "JAN"
						2 = "FEB"
						3 = "MAR"
						4 = "APR"
						5 = "MAY"
						6 = "JUN"
						7 = "JUL"
						8 = "AUG"
						9 = "SEP"
						10= "OCT"
						11= "NOV"
						12= "DEC"
						;
run;
	
Data Answer;
	set Question;
	*Extract Date from Date_Time_Variable;
	Date  = DatePart(Date_Time_Variable);
	*Extracting Month From Date using Month Function that will return corresponding month number (1 - 12);
	MonthNumber = Month(Date);
	*Creating another variable Month that will use the above created 
Format; Month = put(MonthNumber,MonthName.); format date date9.; run;
znhnm
Quartz | Level 8
Thanks lot for this! I was trying to find a solution to have it on both number on 3-letter format. I have it both with your way which is amazing.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 715 views
  • 3 likes
  • 5 in conversation