All,
I have a folder of macros. I am adding these macros to my SASAUTOS as demonstrated in sample note (http://support.sas.com/kb/42/360.html) .
If I rerun my code, more than once, this results in an error as the same filename is being repeatedly added. So, I would like to add a check point in my code to test if a particular filename exists in my SASAUTOS , skip over to next line, else add it. Can somebody advice on how I could achieve this ?
Below is what I have in mind, but I am new to SAS and dont know how to achieve this.
filename MacroDir <customfolder>;
if <MacroDir in SASAUTOS> Then
Options append = Sasautos = (MacroDir); /* Reference sample 42360 */
Else
Put 'Filename already in SASAutos';
You normally would define and add to your SASAUTOS in the SAS config or autoexec which gets only executed once during SAS invocation.
If you just want to have something in your code then yes, you need to check. The "challenge" is that the OPTIONS statement is a Global statement and you can't use it within a SAS Data step and though not conditionally in an IF statement. That means you need SAS macro language which is a bit much for a newbie.
Below code should work for you. The code doesn't handle all possible use cases but it should be sufficient for what you're asking for.
%macro add_to_sasautos(newlib);
data _null_;
if find(getoption('sasautos'),"&newlib",'i')>0 then
do;
putlog "&newlib already in SASAUTOS";
call symputx('add_newlib','N','L');
end;
else
do;
call symputx('add_newlib','Y','L');
end;
run;
%if &add_newlib=Y %then
%do;
options append=sasautos=(&newlib);
%end;
%mend;
filename mymacs2 'c:\temp';
%add_to_sasautos(mymacs2)
As a newbie: PLEASE don't start with SAS Macro coding too early. Get first really familiar with normal Base SAS Language and Concepts and some of the SAS Procedures. Especially newcomers tend to make a "huge mess" using macro coding only because they don't really understand yet how the Base SAS language works.
@UdayGuntupalli wrote:
All,
I have a folder of macros. I am adding these macros to my SASAUTOS as demonstrated in sample note (http://support.sas.com/kb/42/360.html) .If I rerun my code, more than once, this results in an error as the same filename is being repeatedly added. So, I would like to add a check point in my code to test if a particular filename exists in my SASAUTOS , skip over to next line, else add it. Can somebody advice on how I could achieve this ?
Below is what I have in mind, but I am new to SAS and dont know how to achieve this.filename MacroDir <customfolder>; if <MacroDir in SASAUTOS> Then Options append = Sasautos = (MacroDir); /* Reference sample 42360 */ Else Put 'Filename already in SASAutos';
There should be no need to do anything after the first Options statement. Any new macros in that folder should be found. Just as you do not need to issue a libname statement to reference each data set within a library.
Typically such an option would only be run one time before using any of the macros in the folder. Why are you rerunning the code?
You can get the current value of the SASAUTOS setting into a macro variable using:
proc sql noprint; select setting into :autos from sashelp.voption where optname='SASAUTOS' ; quit; %put &autos;
There are likely to be some fun issues with the comparison depending on the actual path to the folder. %sysfunc(index(&autos,customfolder)) might work.
@ballardw,
The wrapper I am developing uses the macros I already developed and as I test the developed code, I am re-running the wrapper multiple times to test each new piece of code I add.
Are you suggesting something like this:
filename MacroDir <customfolder>;
proc sql noprint;
select setting into :autos
from sashelp.voption
where optname='SASAUTOS';
quit;
Data _Null_;
If %sysfunc(index(&autos,MacroDir)) > 0
Then Options append = Sasautos = (MacroDir); /* Reference sample 42360 */
Else
%Put 'MacroDir already in SasAutos';
Run;
I tried this and I am seeing a couple of errors in the Data _Null_ step - "Statement is not valid or it is used out of proper order".
I am not sure if I am missing any semicolons or I misinterpreted your advice.
You normally would define and add to your SASAUTOS in the SAS config or autoexec which gets only executed once during SAS invocation.
If you just want to have something in your code then yes, you need to check. The "challenge" is that the OPTIONS statement is a Global statement and you can't use it within a SAS Data step and though not conditionally in an IF statement. That means you need SAS macro language which is a bit much for a newbie.
Below code should work for you. The code doesn't handle all possible use cases but it should be sufficient for what you're asking for.
%macro add_to_sasautos(newlib);
data _null_;
if find(getoption('sasautos'),"&newlib",'i')>0 then
do;
putlog "&newlib already in SASAUTOS";
call symputx('add_newlib','N','L');
end;
else
do;
call symputx('add_newlib','Y','L');
end;
run;
%if &add_newlib=Y %then
%do;
options append=sasautos=(&newlib);
%end;
%mend;
filename mymacs2 'c:\temp';
%add_to_sasautos(mymacs2)
As a newbie: PLEASE don't start with SAS Macro coding too early. Get first really familiar with normal Base SAS Language and Concepts and some of the SAS Procedures. Especially newcomers tend to make a "huge mess" using macro coding only because they don't really understand yet how the Base SAS language works.
@Patrick,
Thank you for taking the time to answer my question. I appreciate your valuable advice and will try to follow it as much as I can.
As for the problem at hand, I had to make an edit (guided by a colleague) to get your proposed solution to work for my case i.e. rerunning my wrapper script repeatedly. I am marking it as complete, but I wanted to report my learning for the rest of the community.
%macro add_to_sasautos(newlib);
data _null_;
if find(getoption('sasautos'),"&newlib",'i') > 0 then
do;
putlog "&newlib already in SASAUTOS";
call symputx('add_newlib','N','L');
end;
else
do;
putlog "&newlib does not exist in SASAUTOS";
call symputx('add_newlib','Y','L');
end;
run;
%if &add_newlib=Y %then
%do;
options append=sasautos=(&newlib);
%end;
%mend;
*filename MacroDir <customdir>;
%LET MacroDir <customdir>;
%add_to_sasautos(&MacroDir);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.