Hello, based on the type of report (operational or financial) I am trying to get the good starting date (see below)
For an operational report, the starting date (debut) should be equal to '02jan2017'd;
For a financial report, the starting date (Debut) should be equal to '31oct2016'd;
Here's my code below but It does work and I don’t know Why.
Does someone could help me with that issue?
%let motifrap=FINANCIER;
*%let MotifRap=OPERATIONNEL;
%let sysyear= %sysfunc(year("&sysdate"d));
%MACRO MotifRap;
%if &motifrap EQ "FINANCIER" %THEN
%DO;
%let Annee=&sysyear;
%let FirstWDay=%sysfunc(NWKDOM(1,2,1,&Annee));
%let FirstWorkingDay=%sysfunc(inputn(&FirstWDay, z8.), date9.);
%let Debut=%sysfunc(compress(%bquote('&FirstWorkingDay'd)));
%END;
%ELSE
%DO;
%let Annee=%eval(&sysyear-1);
%let Debut=%sysfunc(compress(%BQUOTE('31OCT&Annee.'D)));
%END;
RUN;
%mend MotifRap;
%MotifRap;
%put &Annee;
%put &FirstWDay;
%put &FirstWorkingDay;
%put &Debut;
%put &motifrap;
I have found a solution...
Here's the code that is working
/*options mprint symbolgen;*/
%MACRO MotifRap;
%global Annee FirstWday FirstWorkingDay Debut;
%let sysyear= %sysfunc(year("&sysdate"d));
%if &MotifRapport EQ FINANCIER %THEN
%DO;
%let Annee=&sysyear;
%let FirstWDay=%sysfunc(NWKDOM(1,2,1,&Annee));
%let FirstWorkingDay=%sysfunc(inputn(&FirstWDay, z8.), date9.);
%let Debut=%sysfunc(compress(%bquote('&FirstWorkingDay'd)));
%END;
%ELSE
%DO;
%let Annee=%eval(&sysyear-1);
%let Debut=%sysfunc(compress(%BQUOTE('31OCT&Annee.'D)));
%END;
RUN;
%mend MotifRap;
%let MotifRapport = FINANCIER;
%put &MotifRapport;
%MotifRap;
%put &Annee &FirstWDay &FirstWorkingDay &Debut;
%let MotifRapport = OPERATIONAL;
%put &MotifRapport;
%MotifRap;
%put &Annee &Debut;
%I CaNTRea% th
IS code.
Try looking at other questions posted here for how to format code and use the {i} code window above the post are to show code with formatting preserved.
%let motifrap=FINANCIER;
data _null_;
if "&motifrap."="FINANCIER" then
call symput('debut',put(nwkdom(1,2,1,year(today())),date9.));
else call symput('debut',cats("31OCT",year(today())));
run;
%put &debut.;
Would be one example of simplifying that code.
One reason is in your %if/%then:
%if &motifrap EQ "FINANCIER" %THEN
&motifrap = FINANCIER (without quotes), not "FINANCIER" (with quotes). So you need either:
"&motifrap" eq "FINANCIER, or &motifrap = FINANCIER.
I have tried both but it seems that it does not recognize:
%let FirstWDay=%sysfunc(NWKDOM(1,2,1,&Annee));
%let FirstWorkingDay=%sysfunc(inputn(&FirstWDay, z8.), date9.);
%let Debut=%sysfunc(compress(%bquote('&FirstWorkingDay'd)));
Perhaps it is time to set
options mprint symbolgen;
run the code and post the log.
I have found a solution...
Here's the code that is working
/*options mprint symbolgen;*/
%MACRO MotifRap;
%global Annee FirstWday FirstWorkingDay Debut;
%let sysyear= %sysfunc(year("&sysdate"d));
%if &MotifRapport EQ FINANCIER %THEN
%DO;
%let Annee=&sysyear;
%let FirstWDay=%sysfunc(NWKDOM(1,2,1,&Annee));
%let FirstWorkingDay=%sysfunc(inputn(&FirstWDay, z8.), date9.);
%let Debut=%sysfunc(compress(%bquote('&FirstWorkingDay'd)));
%END;
%ELSE
%DO;
%let Annee=%eval(&sysyear-1);
%let Debut=%sysfunc(compress(%BQUOTE('31OCT&Annee.'D)));
%END;
RUN;
%mend MotifRap;
%let MotifRapport = FINANCIER;
%put &MotifRapport;
%MotifRap;
%put &Annee &FirstWDay &FirstWorkingDay &Debut;
%let MotifRapport = OPERATIONAL;
%put &MotifRapport;
%MotifRap;
%put &Annee &Debut;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.