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

Hello,

I would like to get the month of "AUG2022" in the variable "scrdate_n."  The variable format is MONYY7.   Please guild me how; thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@ybz12003 wrote:
Spoiler
if scrdate_n in ('AUG2022'd);

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.


Several ways to check if a date occurs in a given month:

If month(scrdate_n)=8 and year(scrdate_n)=2022 then ....

if put(scrdate_n,monyy7.)='AUG2022' then ...
 
if intnx('month',scrdate_n,0,'B') = '01AUG2022'd then ...

for 3 examples.

View solution in original post

6 REPLIES 6
ybz12003
Rhodochrosite | Level 12
Spoiler
if scrdate_n in ('AUG2022'd);

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.

Tom
Super User Tom
Super User

You cannot have a date without a day of the month.

You could pick any day of the month, but normally people just use the first of the month.

if scrdate_n in ('01AUG2022'd);

But if you really want to test if SCRDATE_N is ANY day in that month then you probably need to work harder.

Try one of these instead.

if put(scrdate_n,monyy7.)  in ('AUG2022');
if intnx('month',scrdate_n,0,'b') in ('01AUG2022'd);
ybz12003
Rhodochrosite | Level 12
I know the day I put was wrong. how to correct it? I only need the month.
Tom
Super User Tom
Super User

If you want to specify a date value you HAVE to have a day of the month.

A number can only have one value.  It cannot have a range of values.

ballardw
Super User

@ybz12003 wrote:
Spoiler
if scrdate_n in ('AUG2022'd);

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.


Several ways to check if a date occurs in a given month:

If month(scrdate_n)=8 and year(scrdate_n)=2022 then ....

if put(scrdate_n,monyy7.)='AUG2022' then ...
 
if intnx('month',scrdate_n,0,'B') = '01AUG2022'd then ...

for 3 examples.

AMSAS
SAS Super FREQ

I recommend you check out the SAS Date, Time, and Datetime Values  documentation 


Here's an example

data work.have ;
	format date monyy. ;
	do date="25jul2023"d to "10Sep2023"d by 7 ;
		output work.have ;
	end ;
run ;

data work.want ;
	set work.have ;
	put "Have : " date= date= date7. date= 8. ;
	if "01Aug2023"d <= date < "01Sep2023"d then do ;
		put "        Want : " date= date= date7. date= 8. ;
		output work.want ;
	end ;
	else
		put "  Don't Want : " date= date= date7. date= 8. ;

run ;

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
  • 1165 views
  • 3 likes
  • 4 in conversation