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!
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);
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.
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);
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: