%macro sort (dname, new, svar) ;
proc sort data=&dname out=&new ;
by &svar;
run;
%Mend;
%sort(sashelp.class, demo1,sex);
data _null_;
if cexist("WORK.SORT") then
put "WORK.SORT exists";
else
put "WORK.SORT does not exist";
run;
Hi Experts,
here i am trying macro catolog exist or not but its giving wrong output please check where i did wrong
@Ksharp wrote:
"According to the documentation %sysmacexist() only searches through work.sasmacr."
That is bad new. If I stored my compiled macro at other path, %sysmacexist() does not work ?
Did you actually test whether %SYSMACEXIST() does not check in the catalog you told it to search for pre-compiled macros? Because the documentation is wrong about only search WORK.SASMACR. Because when you launch SAS via SAS/Studio (or other methods that spawn a new SAS session) then the compiled macros are default placed into WORK.SASMAC1 instead and the %SYSMACEXIST() macro function can find them. Try this code:
%macro mymac;
%put &=sysmacroname;
%mend;
%mymac;
%put %sysmacexist(mymac);
data _null_;
set sashelp.vcatalg;
where objname='MYMAC';
put (_all_) (=/);
run;
There aren't any statements in the code shown creating work.sort, so why should it exist?
in log it shows work.sort does not exist
here i am using CEXIST function to check macro catolog SORT exist or not
@pavank wrote:
in log it shows work.sort does not exist
here i am using CEXIST function to check macro catolog SORT exist or not
I'm a bit confused if you want to check for existence of a SAS table or for existence of a compiled macro in the macro catalog under WORK.
Below code run in a fresh new session to demonstrate the different ways to query hopefully giving you sufficient pointers for what you need.
data check_step_1;
/* does TABLE work.demo1 exist? */
table_exist_flg=exist('work.demo1','data');
/* does compiled macro %sort exist as member in referenced catalog (here: work.sasmacr)? */
macro_compiled_flg_1=cexist('work.sasmacr.sort.macro');
/* does compiled macro %sort exist as member in catalog under WORK? */
macro_compiled_flg_2=%sysmacexist(sort);
run;
title 'check_step_1';
proc print data=check_step_1;
run;
/* execution of the macro definition compiles the macro into the catalog under WORK */
%macro sort (dname, new, svar);
proc sort data=&dname out=&new;
by &svar;
run;
%mend;
data check_step_2;
/* does TABLE work.demo1 exist? */
table_exist_flg=exist('work.demo1','data');
/* does compiled macro %sort exist as member in referenced catalog (here: work.sasmacr)? */
macro_compiled_flg_1=cexist('work.sasmacr.sort.macro');
/* does compiled macro %sort exist as member in catalog under WORK? */
macro_compiled_flg_2=%sysmacexist(sort);
run;
title 'check_step_2';
proc print data=check_step_2;
run;
/* calling the macro creates the the table as requested via parameter &new */
%sort(sashelp.class, demo1,sex);
data check_step_3;
/* does TABLE work.demo1 exist? */
table_exist_flg=exist('work.demo1','data');
/* does compiled macro %sort exist as member in referenced catalog (here: work.sasmacr)? */
macro_compiled_flg_1=cexist('work.sasmacr.sort.macro');
/* does compiled macro %sort exist as member in catalog under WORK? */
macro_compiled_flg_2=%sysmacexist(sort);
run;
title 'check_step_3';
proc print data=check_step_3;
run;
title;
%macro sort (dname, new, svar) ;
proc sort data=&dname out=&new ;
by &svar;
run;
%Mend;
%sort(sashelp.class, demo1,sex);
data _NULL_;
if cexist('work.sasmacr.sort.macro') then
put "work.sort exists" ;
else put "work.sort does not exist";
run;
%put %sysfunc(ifc(%sysmacexist(sort), Yes %nrstr("&dname") Exists, %nrstr("&dname") Does not Exist));
please check syntax %put %sysfunc(ifc(%sysmacexist(sort), Yes %nrstr("&dname") Exists, %nrstr("&dname") Does not Exist));
@Ksharp wrote:
It looks like %sysmacexist() only check the user defined macro ,not built-in system macro .
34 %put %sysfunc(ifc(%sysmacexist(scan), Yes Exists, Does not Exist));
Does not Exist
It looks like %scan() is not a traditional macro. At least I can't find it as a member of any macro catalog under SASROOT.
According to the documentation %sysmacexist() only searches through work.sasmacr. It would still find SAS supplied macros like %tslit() or %lowcase() that only get compiled into work.sasmacr when called the first time in a session.
@Ksharp wrote:
"According to the documentation %sysmacexist() only searches through work.sasmacr."
That is bad new. If I stored my compiled macro at other path, %sysmacexist() does not work ?
Did you actually test whether %SYSMACEXIST() does not check in the catalog you told it to search for pre-compiled macros? Because the documentation is wrong about only search WORK.SASMACR. Because when you launch SAS via SAS/Studio (or other methods that spawn a new SAS session) then the compiled macros are default placed into WORK.SASMAC1 instead and the %SYSMACEXIST() macro function can find them. Try this code:
%macro mymac;
%put &=sysmacroname;
%mend;
%mymac;
%put %sysmacexist(mymac);
data _null_;
set sashelp.vcatalg;
where objname='MYMAC';
put (_all_) (=/);
run;
Tom,
I am using stand-alone PC version SAS, and not SAS Studio.
I means if I store the complied macro at other path (not WORK library), %sysmacexist() would not work.
Check the following example to see what I mean.
libname x 'c:\temp';
option mstored sasmstore=x;
%macro mymac/store;
%put &=sysmacroname;
%mend;
%mymac;
%put NOTE: %sysmacexist(mymac);
So another good reason to not use stored compiled macro catalogs and instead just stick with autocall libraries.
@Ksharp wrote:
Tom,
But sometime we need to distribute my macro to other sas programmer,and not want to let others to see raw code,usually would secure these macro.
like:
%macro mymac/store secure;
@Ksharp I don't believe you're someone who has to hide his code for job security so really interested why you can't share it. The only very few times I had to use secure compiled macros was for situations where I couldn't avoid having a password in the code.
The only other reason I'm currently aware of for pre-compiling: Depending on the environment it can take a bit until a .sas file is found for compilation so for some interactive web application the reason for pre-compiling could be performance. I personally was never in such a situation but I also mostly implement backend processes.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.