BookmarkSubscribeRSS Feed
rliu0712
Calcite | Level 5

Hi, I am trying to extract only the months from these dates in a new column labelled as "Month" in PROC SQL. 

Screen Shot 2021-10-21 at 4.30.24 PM.png

 

Could someone please help me?

6 REPLIES 6
Reeza
Super User
Is it a numeric or character column?

If it's numeric with a date format use the MONTH() function, if it's character use SCAN()

E.G. select month(dateVariable) as monthNum, scan(month, 2, "-") as monthChar
rliu0712
Calcite | Level 5

it is in "2020-03-21" format.. 

 

month(dateVariable) as monthNum --> do I replace "dateVariable" with my column name?

Reeza
Super User

@rliu0712 wrote:

it is in "2020-03-21" format.. 

 


SAS has variable types and formats. 

Types can be numeric or character and formats control how a variable is displayed. Your answer doesn't answer the question asked - is your variable numeric or character. Either can appear as you've shown but how they're stored can differ.

 

data demo;
dateCharacter = "2020-03-21";
date_numeric = "21Mar2020"d;
format date_numeric yymmddd10.;
run;

title 'Can you tell the difference between numeric and character dates?';
proc print data=demo;
run;

@rliu0712 wrote:

 

month(dateVariable) as monthNum --> do I replace "dateVariable" with my column name?


Yes, replace it with your variable name.

 

rliu0712
Calcite | Level 5

Yes, I just figured out it is numeric with format YYMMDD10. 

 

the "select month(dateVariable) as monthNum from table" does not seem to work.. 

Reeza
Super User
Define not work.
What are you trying to do overall that isn't working?
Show your code and log please.
ballardw
Super User

Depending on what you are doing if you have an actual date value you may not need a new variable. SAS formatted values are honored by reporting and most graphing and analysis procedures. Possibly just using the YEAR4. format in the step you want to use the data will work.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 5317 views
  • 2 likes
  • 3 in conversation