<?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 macro Check_DataSet_Exist in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959581#M374375</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Please see following code.&lt;/P&gt;
&lt;P&gt;Some questions please-&lt;/P&gt;
&lt;P&gt;1- Is this code check if data set exist or check if sas can open the data set?&lt;/P&gt;
&lt;P&gt;2-What is the difference between the question if data set exist and if data set can be opened? As I understand If data set exist then sas can open it and if not exist then sas cannot open it because it doesnt exist&lt;/P&gt;
&lt;P&gt;3- What is the meaning of&amp;nbsp;%let rc=%sysfunc(close(&amp;amp;dsid));&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is it not written as&amp;nbsp;%let rc=%sysfunc(close(&lt;STRONG&gt;&amp;amp;tbl&lt;/STRONG&gt;));&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does it mean that we want that SAS close the open data set?&lt;/P&gt;
&lt;P&gt;What is it beneficial to have this statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Check_DataSet_Exist(tbl);     
%let dsid=%sysfunc(open(&amp;amp;tbl,i));     
%put dsid=&amp;amp;dsid;
%if (&amp;amp;dsid=0) %then  %put &amp;amp;tbl data set has NOT been opened;                                                                                                             
%else %put &amp;amp;tbl data set has been opened;                                                                                                  
%let rc=%sysfunc(close(&amp;amp;dsid));                                                                                                        
%mend Check_DataSet_Exist;  
%Check_DataSet_Exist(tbl=sashelp.cars) 
%Check_DataSet_Exist(tbl=sashelp.weather) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Feb 2025 10:28:05 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2025-02-19T10:28:05Z</dc:date>
    <item>
      <title>macro Check_DataSet_Exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959581#M374375</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Please see following code.&lt;/P&gt;
&lt;P&gt;Some questions please-&lt;/P&gt;
&lt;P&gt;1- Is this code check if data set exist or check if sas can open the data set?&lt;/P&gt;
&lt;P&gt;2-What is the difference between the question if data set exist and if data set can be opened? As I understand If data set exist then sas can open it and if not exist then sas cannot open it because it doesnt exist&lt;/P&gt;
&lt;P&gt;3- What is the meaning of&amp;nbsp;%let rc=%sysfunc(close(&amp;amp;dsid));&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is it not written as&amp;nbsp;%let rc=%sysfunc(close(&lt;STRONG&gt;&amp;amp;tbl&lt;/STRONG&gt;));&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does it mean that we want that SAS close the open data set?&lt;/P&gt;
&lt;P&gt;What is it beneficial to have this statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Check_DataSet_Exist(tbl);     
%let dsid=%sysfunc(open(&amp;amp;tbl,i));     
%put dsid=&amp;amp;dsid;
%if (&amp;amp;dsid=0) %then  %put &amp;amp;tbl data set has NOT been opened;                                                                                                             
%else %put &amp;amp;tbl data set has been opened;                                                                                                  
%let rc=%sysfunc(close(&amp;amp;dsid));                                                                                                        
%mend Check_DataSet_Exist;  
%Check_DataSet_Exist(tbl=sashelp.cars) 
%Check_DataSet_Exist(tbl=sashelp.weather) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 10:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959581#M374375</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-02-19T10:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: macro Check_DataSet_Exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959584#M374377</link>
      <description>&lt;P&gt;RTM&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0q72zlfmfjplon1p3nprf85sdin.htm" target="_self" rel="nofollow noreferrer"&gt;OPEN Function&lt;/A&gt;&amp;nbsp;and try it.&lt;/P&gt;
&lt;P&gt;When reading the docu it's also always worth to pay some attention to the See Also section.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsid=%sysfunc(open(sashelp.does_not_exist));
%put dsid=&amp;amp;dsid;
%if &amp;amp;dsid = 0 %then 
  %do;
    %put %sysfunc(sysmsg());
  %end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1739962193178.png" style="width: 596px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104745i178B964C01AA6699/image-dimensions/596x167?v=v2" width="596" height="167" role="button" title="Patrick_0-1739962193178.png" alt="Patrick_0-1739962193178.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What above test tells us:&lt;BR /&gt;The open function returns zero if it wasn't successful but it doesn't tell us why. You can retrieve the why using function sysmsg()&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 10:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959584#M374377</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-02-19T10:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: macro Check_DataSet_Exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959601#M374383</link>
      <description>&lt;P&gt;For your first two questions I would say the main difference is that the EXIST() function only wants a dataset name but the OPEN() function also allows the use of dataset options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try these calls to the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Check_DataSet_Exist(tbl=sashelp.class)
%Check_DataSet_Exist(tbl=sashelp.class(keep=name))
%Check_DataSet_Exist(tbl=sashelp.class(keep=sam))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last question is obvious if you read the documentation.&amp;nbsp; The CLOSE() function (just like the FETCH(), VARNUM() and other VARxxxx() functions) want the dataset id that was created when the dataset was opened, not the NAME of the dataset.&amp;nbsp; Check out my answer above to your first two questions to see why that is necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note it would be more logical to only call the CLOSE() function when the OPEN() worked.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Check_DataSet_Exist(tbl);
%let dsid=%sysfunc(open(&amp;amp;tbl,i));
%put dsid=&amp;amp;dsid;
%if (&amp;amp;dsid=0) %then  %put &amp;amp;tbl data set has NOT been opened;
%else %do;
  %put &amp;amp;tbl data set has been opened;
  %let rc=%sysfunc(close(&amp;amp;dsid));
%end;
%mend Check_DataSet_Exist;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 14:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959601#M374383</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-19T14:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: macro Check_DataSet_Exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959603#M374385</link>
      <description>&lt;P&gt;If you want to check if a data set exists in a library, you don't need to open it. Use the EXIST function. This example will run the PROC MEANS only if SASHELP.CLASS is found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%if %sysfunc(exist(sashelp.class)) %then %do;
   proc means data=sashelp.class; run;
%end;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if you want to check for a condition where a particular variable exists, you do have to open the data set (or query the DICTIONARY tables like SASHELP.VCOLUMN -- plenty of papers/doc on that).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This example will include the variable&amp;nbsp;&lt;STRONG&gt;height&lt;/STRONG&gt; in the analysis only if it is found in the data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%macro VarExist(ds, var);
    %local rc dsid result;
    %let dsid = %sysfunc(open(&amp;amp;ds));
 
    %if %sysfunc(varnum(&amp;amp;dsid, &amp;amp;var)) &amp;gt; 0 %then %do;
        %let result = 1;
        %put NOTE: Var &amp;amp;var exists in &amp;amp;ds;
    %end;
    %else %do;
        %let result = 0;
        %put NOTE: Var &amp;amp;var not exists in &amp;amp;ds;
    %end;
 
    %let rc = %sysfunc(close(&amp;amp;dsid));
    &amp;amp;result
%mend VarExist;

proc means data=sashelp.class;
 var age
%if %VarExist(sashelp.class, height) %then %do;
  height
%end;
 ;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that &lt;A href="https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/" target="_self"&gt;both of these examples leverage the %if/%then[/%else] ability&lt;/A&gt; for using macro conditional expressions in open code, which has been a capability in the language for a long time now...but people forget that and tend to wrap everything in macro routines (force of habit and probably working off decades of old examples).&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 14:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959603#M374385</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2025-02-19T14:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: macro Check_DataSet_Exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959609#M374388</link>
      <description>&lt;P&gt;"&lt;EM&gt;1- Is this code check if data set exist or check if sas can open the data set?&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;The code checks if data set can be opened.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;2-What is the difference between the question if data set exist and if data set can be opened? As I understand If data set exist then sas can open it and if not exist then sas cannot open it because it doesnt exist&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;You understanding is not correct.&lt;/P&gt;
&lt;P&gt;Imagine a situation where there is a library A and there is data set B, but both you and me have access to that library. I start SAS session and I open that data set for preview. Then you start your session and do 2 things: a) you check for data set existence - data sets clearly exists, then b) you try to open it - you can't do that because the data set is already opened by me. So it is possible to have an existing data set that you cannot open.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you modify your code snippet like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Check_DataSet_Exist(tbl);     
%let dsid=%sysfunc(open(&amp;amp;tbl,i));
%put %sysfunc(sysmsg()); /* NEW LINE */ 
%put dsid=&amp;amp;dsid;
%if (&amp;amp;dsid=0) %then  %put &amp;amp;tbl data set has NOT been opened;                                                                                                             
%else %put &amp;amp;tbl data set has been opened;                                                                                                  
%let rc=%sysfunc(close(&amp;amp;dsid));                                                                                                        
%mend Check_DataSet_Exist;  
%Check_DataSet_Exist(tbl=sashelp.cars) 
%Check_DataSet_Exist(tbl=sashelp.weather) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you will see in the log that the reason why:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;%Check_DataSet_Exist(tbl=sashelp.weather) &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;returned 0 is:&lt;/P&gt;
&lt;P&gt;"ERROR: File SASHELP.WEATHER.DATA does not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;3- What is the meaning of&amp;nbsp;%let rc=%sysfunc(close(&amp;amp;dsid));&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;Good programming practice that can prevent a lot of pain and stress is, after opening a data set (with the OPEN() function), to close the data set (with CLOSE() function) to "un-(b)lock" it for other users.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;Why is it not written as&amp;nbsp;%let rc=%sysfunc(close(&lt;STRONG&gt;&amp;amp;tbl&lt;/STRONG&gt;));&amp;nbsp;&lt;/EM&gt;"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My answer is short "RTFM", documentation clearly states:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"data-set-id&amp;nbsp;&lt;SPAN&gt;is a numeric variable that specifies the data set identifier that the OPEN function returns.&lt;/SPAN&gt;"&lt;/P&gt;
&lt;P&gt;Link:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0bbb0j41xxin4n1fdpqlf4gze0w.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0bbb0j41xxin4n1fdpqlf4gze0w.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;Does it mean that we want that SAS close the open data set?&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;Yes, we want SAS to close opened data set, as I wrote above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;What is it beneficial to have this statement?&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;Without closing (previously opened) data set you may have problem with accessing it in next steps of your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 14:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Check-DataSet-Exist/m-p/959609#M374388</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-02-19T14:57:52Z</dc:date>
    </item>
  </channel>
</rss>

