- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I am trying to extract only the months from these dates in a new column labelled as "Month" in PROC SQL.
Could someone please help me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
it is in "2020-03-21" format..
month(dateVariable) as monthNum --> do I replace "dateVariable" with my column name?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I just figured out it is numeric with format YYMMDD10.
the "select month(dateVariable) as monthNum from table" does not seem to work..
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What are you trying to do overall that isn't working?
Show your code and log please.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.