BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pavank
Quartz | Level 8
%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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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;

 

View solution in original post

21 REPLIES 21
andreas_lds
Jade | Level 19

There aren't any statements in the code shown creating work.sort, so why should it exist?

pavank
Quartz | Level 8

in log it shows  work.sort does not exist 

here i am using CEXIST function to check macro catolog SORT exist or not

Patrick
Opal | Level 21

@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;

Patrick_0-1716430803466.png

 

Ksharp
Super User
1) You could use dictionary CATALOGS to check if the macro is exist.

proc sql;
create table catalog as
select * from dictionary.catalogs;
quit;

2)You also could use function fileexist() to check .
pavank
Quartz | Level 8
%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
Super User
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

Patrick
Opal | Level 21

@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


@Ksharp 

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
Super User
"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 ?
Patrick
Opal | Level 21
That’s how I read the docu, yes.
Tom
Super User Tom
Super User

@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;

 

Ksharp
Super User

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); 

Ksharp_0-1716510957811.png

 

 

Tom
Super User Tom
Super User

So another good reason to not use stored compiled macro catalogs and instead just stick with autocall libraries.

Ksharp
Super User
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;
Patrick
Opal | Level 21

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 21 replies
  • 924 views
  • 6 likes
  • 5 in conversation