Hello,
Can you help me for the if statement of this program. my date format is yymmn6.
data data_IMF_PCSS;
set data_IMF_PCSS;
if sas_date between '202001' and '202112'D then ID=1 else ID=0 ;
run;
You cannot use use BETWEEN with an IF statement. That will only work in a WHERE statement.
You need to end your IF statement with a semicolon.
If the variable has actual date values with the YYMMN6. format attached so that they display as values like 202203 then you need to use actual data values in your IF statement.
Not the string you used for the lower bound on the BETWEEN and not the invalid date literal you used as the upper bound. The text used in a date literal has to be something the DATE informat will understand.
Try this:
data data_IMF_PCSS;
set data_IMF_PCSS;
if '01JAN2020'd <= sas_date < '01JAN2022'd then ID=1;
else ID=0 ;
run;
@sasphd wrote:
Hello,
Can you help me for the if statement of this program. my date format is yymmn6.
data data_IMF_PCSS; set data_IMF_PCSS; if sas_date between '202001' and '202112'D then ID=1 else ID=0 ; run;
Date literals are ALWAYS this format
'01JAN2022'd
You could use mixed or lower case, that will work. You could use two digit years (but don't). Other formatting of date literals will fail (as you have seen).
The format of the variable SAS_DATE is irrelevant. Just because SAS_DATE is yymmn6, the date literals should NOT and cannot be yymmn6., they must be in the format shown, 'DDMONYYYY'd
if sas_date between '202001' and '202112'D then ID=1 else ID=0 ;
There are other SAS syntax errors in the above line of code as well, all of these syntax must be fixed, including the date literals.
You cannot use use BETWEEN with an IF statement. That will only work in a WHERE statement.
You need to end your IF statement with a semicolon.
If the variable has actual date values with the YYMMN6. format attached so that they display as values like 202203 then you need to use actual data values in your IF statement.
Not the string you used for the lower bound on the BETWEEN and not the invalid date literal you used as the upper bound. The text used in a date literal has to be something the DATE informat will understand.
Try this:
data data_IMF_PCSS;
set data_IMF_PCSS;
if '01JAN2020'd <= sas_date < '01JAN2022'd then ID=1;
else ID=0 ;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.