DATA WORK.HAVE;
INFILE DATALINES DELIMITER =',';
FORMAT Date YYMMDD10. ;
INPUT DATE YYMMDD10. X ;
CARDS;
2021-08-17,99,
2021-09-01,505,
2021-10-05,91,
2021-11-05,65,
2021-12-07,250,
2022-01-01,704,
2022-02-02,687,
2022-03-03,123,
2022-04-03,456,
2022-05-06,789,
2022-06-16,687,
2022-07-27,123,
2022-08-05,400,
2022-09-05,700,
2022-10-22,850,
2022-11-18,700,
2022-12-07,120,
2023-01-27,123,
2023-02-05,400,
2023-03-07,700,
2023-04-22,850,
2023-05-18,700,
2023-06-07,120,
;
RUN;
DATA have;
SET have;
row= _n_;
RUN;
DATA cond_1;
FORMAT today YYMMDD10.;
SET have;
row= _n_;
/*If today's date is... */
today = '15MAR2022'd;
/*Then these rows are the rows I want */
IF row >2 AND row < 9 THEN DO;
condition_1 = 'Yes';
OUTPUT;
END;
RUN;
DATA cond_2;
FORMAT today YYMMDD10.;
SET have;
row= _n_;
/*If today's date is... */
today = '15MAY2023'd;
/*Then these rows are the rows I want */
IF row >8 AND row < 22 THEN DO;
condition_2 = 'Yes';
OUTPUT;
END;
RUN;
How do I change a WHERE/ or IF THEN-condition depending on which date it is?
I have two conditions:
Condition 1
If today's date is between 1st of JAN 2022 to 31st of OCT 2022 = table contains data from
1st of OCT 2021 (WHERE DATE >= ‘01OCT2021’d)
Condition 2
If today's date is greater than or equal to 1st of NOV 2022 = table contains data within a period of 13 months
(WHERE DATE >= INTNX(‘month’, XXX, -13, ‘B’)
Se my example tables. I use variable row just to illustrate the rows I want.
Try this:
data want,
set have;
if '01jan2022'd le today() le '31oct2022'd
then cutoff = '01oct2021'd,
else cutoff = intnx('month',today(),-13,'b');
if date ge cutoff;
drop cutoff;
run;
Try this:
data want,
set have;
if '01jan2022'd le today() le '31oct2022'd
then cutoff = '01oct2021'd,
else cutoff = intnx('month',today(),-13,'b');
if date ge cutoff;
drop cutoff;
run;
Played around a bit with your code and it worked like a charm!
Thanks Kurt.
😁
%LET generic_today=today()+780;
data want;
FORMAT generic_today Cutoff YYMMDD10.;
set have;
generic_today = &generic_today;
if '01jan2022'd le generic_today le '31oct2022'd
then cutoff = '01oct2021'd;
else cutoff = intnx('month',generic_today,-13,'b');
if date ge cutoff;
/*drop cutoff;*/
run;
I really hope that your program documentation discusses exactly why you have a +780 in it.
Such magic numbers are one of the banes of the next person to use your code, or if you don't revisit it for a long time, especially if the macro variables are set somewhere not in close proximity to the code where it is used.
@Pili1100 wrote:
Played around a bit with your code and it worked like a charm!
Thanks Kurt.
😁
%LET generic_today=today()+780; data want; FORMAT generic_today Cutoff YYMMDD10.; set have; generic_today = &generic_today; if '01jan2022'd le generic_today le '31oct2022'd then cutoff = '01oct2021'd; else cutoff = intnx('month',generic_today,-13,'b'); if date ge cutoff; /*drop cutoff;*/ run;
I was trying lots of different number so as to change the date, for the the geneteric_today date. +780 was the last one I tried
Anything thing particular you think I should add? 🙂
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.