<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Exist function and xlsx file in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632647#M35709</link>
    <description>&lt;P&gt;The XLSX engine certainly seems to have some issues with the meta functions (such as EXIST) that the EXCEL engine does not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you switch to EXCEL engine ?&lt;/P&gt;
&lt;P&gt;If so, the &lt;EM&gt;sheet name&lt;/EM&gt;&amp;nbsp;is referenced as a data set using&amp;nbsp;&lt;EM&gt;sheet name with $ suffix. &lt;/EM&gt;When the sheet name is a 'non-name' construct (spaces, punctuation, etc) the referencing must be as a name literal. I.e.&amp;nbsp; &lt;EM&gt;excel-libname."sheet name$"n&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you must use XLSX engine, you could write your own existence checking macro.&amp;nbsp; Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro can_open(data);
  %local dsid rc;
  %let dsid = %sysfunc(open(&amp;amp;data));
  %if &amp;amp;dsid %then %let rc = %sysfunc(close(&amp;amp;dsid));
  %eval(&amp;amp;dsid &amp;gt; 0)
%mend;

libname sample XLSX 'sample.xlsx';

%put can_open=%can_open(sample.first) sample.first;
%put can_open=%can_open(sample.first$) sample.first$;
%put can_open=%can_open(sample.'2nd place'n) sample.'2nd place'n;
%put can_open=%can_open(sample.Does_not_exist) sample.Does_not_exist;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sample code demonstrating weakness of XLSX engine, with regard to EXIST(), compared to strength of EXCEL engine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods _all_ close;

ods excel file='sample.xlsx';

ods excel options(sheet_name = 'First');
proc print data=sashelp.class;
run;

ods excel options(sheet_name = '2nd place');
proc print data=sashelp.class;
run;

ods excel options(sheet_name = '3rd_floor');
proc print data=sashelp.class;
run;

ods noresults;  * turn off 'auto-open';
ods excel close;
ods results;

options nonotes nosource;
libname sample xlsx 'sample.xlsx';

data _null_;
  array names[16] $20 (
    'first',     '"first"n',     'first$',     '"first$"n',
    '2nd place', '"2nd place"n', '2nd place$', '"2nd place$"n',
    '3rd_floor', '"3rd_floor"n', '3rd_floor$', '"3rd_floor$"n',
    '4th sheet', '"4th sheet"n', '4th sheet$', '"4th place$"n'
  );

  put / 30*'-' / 'XLSX engine - WEAK EXIST() !' / 30*'-' /;

  do index=1 to dim(names);
    name = names[index];
    exist = exist('sample.' || name);
    put name  @20 exist=;
    if mod(index,4)=0 then put;
  end;
run;

libname sample EXCEL 'sample.xlsx';

data _null_;
  array names[16] $20 (
    'first',     '"first"n',     'first$',     '"first$"n',
    '2nd place', '"2nd place"n', '2nd place$', '"2nd place$"n',
    '3rd_floor', '"3rd_floor"n', '3rd_floor$', '"3rd_floor$"n',
    '4th sheet', '"4th sheet"n', '4th sheet$', '"4th place$"n'
  );

  put / 30*'-' / 'EXCEL engine - PROPER EXIST() !' / 30*'-' /;

  do index=1 to dim(names);
    name = names[index];
    exist = exist('sample.' || name);
    put name  @20 exist=;
    if mod(index,4)=0 then put;
  end;
run;

libname sample;
options notes source;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;PRE&gt;------------------------------
XLSX engine - WEAK EXIST() !
------------------------------

first              exist=1
"first"n           exist=1
first$             exist=1
"first$"n          exist=1

2nd place          exist=0
"2nd place"n       exist=1
2nd place$         exist=0
"2nd place$"n      exist=1

3rd_floor          exist=1
"3rd_floor"n       exist=1
3rd_floor$         exist=1
"3rd_floor$"n      exist=1

4th sheet          exist=0
"4th sheet"n       exist=1
4th sheet$         exist=0
"4th place$"n      exist=1

------------------------------
EXCEL engine - PROPER EXIST() !
------------------------------

first              exist=0
"first"n           exist=0
first$             exist=1
"first$"n          exist=1

2nd place          exist=0
"2nd place"n       exist=0
2nd place$         exist=0
"2nd place$"n      exist=1

3rd_floor          exist=0
"3rd_floor"n       exist=0
3rd_floor$         exist=1
"3rd_floor$"n      exist=1

4th sheet          exist=0
"4th sheet"n       exist=0
4th sheet$         exist=0
"4th place$"n      exist=0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Mar 2020 12:58:15 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-03-17T12:58:15Z</dc:date>
    <item>
      <title>Exist function and xlsx file</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632618#M35706</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is an example code. I have a&amp;nbsp;xlsx file with multiple sheets. One&amp;nbsp;sheet name its given to me (&lt;STRONG&gt;&lt;EM&gt;dsname&lt;/EM&gt;&lt;/STRONG&gt;) and i would like to control if its correct or not. If it is, do a set. I use a macro because its a recursive action.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let dsname=AE; /*Given to me*/

%macro doASet(name);&lt;BR /&gt;&lt;BR /&gt;libname test xlsx "C:\nameOfFile"; 
&lt;BR /&gt;%if %sysfunc(exist(test.&amp;amp;name.)) %then %do;

	data out;
		set test."&amp;amp;name"n; /*for example*/
		a = 8;
	run;
%end;
%else %put ---Data set &amp;amp;name does not exist.;
%mend doASet;&lt;BR /&gt;
%opends(dsname);&lt;/PRE&gt;&lt;P&gt;If the sheet exist, i do a data set and its all correct but if the sheet doesn't i wish to display and error like that or do another thing,&amp;nbsp;but actually &lt;STRONG&gt;exist&lt;/STRONG&gt; funct return 1. Always return 1!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried tones of combinations and I don't find the solution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thank you in advance for your help. Regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 11:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632618#M35706</guid>
      <dc:creator>centilen</dc:creator>
      <dc:date>2020-03-17T11:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Exist function and xlsx file</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632624#M35707</link>
      <description>&lt;P&gt;I couldn't get the EXIST() function to work either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, PROC CONTENTS will find the names of the tabs in the Excel file and put them in a data set which you can search.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods output members=members;
proc contents data=test._all_ out=_contents_ nods;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Mar 2020 11:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632624#M35707</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-17T11:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Exist function and xlsx file</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632647#M35709</link>
      <description>&lt;P&gt;The XLSX engine certainly seems to have some issues with the meta functions (such as EXIST) that the EXCEL engine does not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you switch to EXCEL engine ?&lt;/P&gt;
&lt;P&gt;If so, the &lt;EM&gt;sheet name&lt;/EM&gt;&amp;nbsp;is referenced as a data set using&amp;nbsp;&lt;EM&gt;sheet name with $ suffix. &lt;/EM&gt;When the sheet name is a 'non-name' construct (spaces, punctuation, etc) the referencing must be as a name literal. I.e.&amp;nbsp; &lt;EM&gt;excel-libname."sheet name$"n&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you must use XLSX engine, you could write your own existence checking macro.&amp;nbsp; Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro can_open(data);
  %local dsid rc;
  %let dsid = %sysfunc(open(&amp;amp;data));
  %if &amp;amp;dsid %then %let rc = %sysfunc(close(&amp;amp;dsid));
  %eval(&amp;amp;dsid &amp;gt; 0)
%mend;

libname sample XLSX 'sample.xlsx';

%put can_open=%can_open(sample.first) sample.first;
%put can_open=%can_open(sample.first$) sample.first$;
%put can_open=%can_open(sample.'2nd place'n) sample.'2nd place'n;
%put can_open=%can_open(sample.Does_not_exist) sample.Does_not_exist;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sample code demonstrating weakness of XLSX engine, with regard to EXIST(), compared to strength of EXCEL engine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods _all_ close;

ods excel file='sample.xlsx';

ods excel options(sheet_name = 'First');
proc print data=sashelp.class;
run;

ods excel options(sheet_name = '2nd place');
proc print data=sashelp.class;
run;

ods excel options(sheet_name = '3rd_floor');
proc print data=sashelp.class;
run;

ods noresults;  * turn off 'auto-open';
ods excel close;
ods results;

options nonotes nosource;
libname sample xlsx 'sample.xlsx';

data _null_;
  array names[16] $20 (
    'first',     '"first"n',     'first$',     '"first$"n',
    '2nd place', '"2nd place"n', '2nd place$', '"2nd place$"n',
    '3rd_floor', '"3rd_floor"n', '3rd_floor$', '"3rd_floor$"n',
    '4th sheet', '"4th sheet"n', '4th sheet$', '"4th place$"n'
  );

  put / 30*'-' / 'XLSX engine - WEAK EXIST() !' / 30*'-' /;

  do index=1 to dim(names);
    name = names[index];
    exist = exist('sample.' || name);
    put name  @20 exist=;
    if mod(index,4)=0 then put;
  end;
run;

libname sample EXCEL 'sample.xlsx';

data _null_;
  array names[16] $20 (
    'first',     '"first"n',     'first$',     '"first$"n',
    '2nd place', '"2nd place"n', '2nd place$', '"2nd place$"n',
    '3rd_floor', '"3rd_floor"n', '3rd_floor$', '"3rd_floor$"n',
    '4th sheet', '"4th sheet"n', '4th sheet$', '"4th place$"n'
  );

  put / 30*'-' / 'EXCEL engine - PROPER EXIST() !' / 30*'-' /;

  do index=1 to dim(names);
    name = names[index];
    exist = exist('sample.' || name);
    put name  @20 exist=;
    if mod(index,4)=0 then put;
  end;
run;

libname sample;
options notes source;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;PRE&gt;------------------------------
XLSX engine - WEAK EXIST() !
------------------------------

first              exist=1
"first"n           exist=1
first$             exist=1
"first$"n          exist=1

2nd place          exist=0
"2nd place"n       exist=1
2nd place$         exist=0
"2nd place$"n      exist=1

3rd_floor          exist=1
"3rd_floor"n       exist=1
3rd_floor$         exist=1
"3rd_floor$"n      exist=1

4th sheet          exist=0
"4th sheet"n       exist=1
4th sheet$         exist=0
"4th place$"n      exist=1

------------------------------
EXCEL engine - PROPER EXIST() !
------------------------------

first              exist=0
"first"n           exist=0
first$             exist=1
"first$"n          exist=1

2nd place          exist=0
"2nd place"n       exist=0
2nd place$         exist=0
"2nd place$"n      exist=1

3rd_floor          exist=0
"3rd_floor"n       exist=0
3rd_floor$         exist=1
"3rd_floor$"n      exist=1

4th sheet          exist=0
"4th sheet"n       exist=0
4th sheet$         exist=0
"4th place$"n      exist=0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 12:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Exist-function-and-xlsx-file/m-p/632647#M35709</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-17T12:58:15Z</dc:date>
    </item>
  </channel>
</rss>

