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.
@ybz12003 wrote:
Spoilerif 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.
if scrdate_n in ('AUG2022'd);
The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.
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);
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.
@ybz12003 wrote:
Spoilerif 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.
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 ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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: