<?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 I need to check whether the variable is available if yes means I need to save that dataset in output in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/I-need-to-check-whether-the-variable-is-available-if-yes-means-I/m-p/931284#M44864</link>
    <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having a database which contains more than 50 datasets. I need to check in which dataset a specific variable is available and also in a new dataset I need to save the result as 'yes' or 'no'.&lt;/P&gt;
&lt;P&gt;Please guide me on below query&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I wrote a code. But, I am seeing an empty dataset. I want results dataset as dataset name and Yes or no value in variables_exists variable.&lt;/LI&gt;
&lt;LI&gt;If I want to search multiple variable means how I need to tweak my code&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MEMNAME INTO :DATASET_LIST SEPARATED BY ' '&lt;BR /&gt;FROM SASHELP.VTABLE&lt;BR /&gt;WHERE LIBNAME='STPSAMP';&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;DATA RESULTS;&lt;BR /&gt;LENGTH DATASET $32. VARIABLE_EXISTS $3.;&lt;BR /&gt;STOP;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;%MACRO CHECK_VARIABLE(VAR_NAME);&lt;/P&gt;
&lt;P&gt;%LET DS_COUNT=%SYSFUNC(COUNTW(&amp;amp;DATASET_LIST.));&lt;BR /&gt;%DO i=1 %TO &amp;amp;DS_COUNT.;&lt;BR /&gt;%LET DATASET=%SCAN(&amp;amp;DATASET_LIST.,&amp;amp;i.);&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MEMNAME,COUNT(*) INTO : DT_NAME, :VAR_EXISTS&lt;BR /&gt;FROM DICTIONARY.COLUMNS&lt;BR /&gt;WHERE LIBNAME='STPSAMP' AND MEMNAME="&amp;amp;DATASET."&lt;BR /&gt;AND UPPER(NAME)="&amp;amp;VAR_NAME.";&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;DATA _NULL_;&lt;BR /&gt;DATASET=&amp;amp;DATASET.;&lt;BR /&gt;VARIABLE_EXISTS=IFC("&amp;amp;VAR_EXISTS."&amp;gt;0,'YES','NO');&lt;BR /&gt;CALL SYMPUT("DATASET",DATASET);&lt;BR /&gt;CALL SYMPUT("VARIABLE_EXISTS",VARIABLE_EXISTS);&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;DATA RESULTS;&lt;BR /&gt;SET RESULTS;&lt;BR /&gt;DATASET=SYMGET(DATASET);&lt;BR /&gt;VARIABLE_EXISTS=SYMGET(VARIABLE_EXISTS);&lt;BR /&gt;OUTPUT;&lt;BR /&gt;RUN;&lt;BR /&gt;%END;&lt;/P&gt;
&lt;P&gt;%MEND CHECK_VARIABLE;&lt;/P&gt;
&lt;P&gt;%CHECK_VARIABLE(QTR);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in Advance&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jun 2024 16:00:33 GMT</pubDate>
    <dc:creator>_el_doredo</dc:creator>
    <dc:date>2024-06-07T16:00:33Z</dc:date>
    <item>
      <title>I need to check whether the variable is available if yes means I need to save that dataset in output</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/I-need-to-check-whether-the-variable-is-available-if-yes-means-I/m-p/931284#M44864</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having a database which contains more than 50 datasets. I need to check in which dataset a specific variable is available and also in a new dataset I need to save the result as 'yes' or 'no'.&lt;/P&gt;
&lt;P&gt;Please guide me on below query&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I wrote a code. But, I am seeing an empty dataset. I want results dataset as dataset name and Yes or no value in variables_exists variable.&lt;/LI&gt;
&lt;LI&gt;If I want to search multiple variable means how I need to tweak my code&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MEMNAME INTO :DATASET_LIST SEPARATED BY ' '&lt;BR /&gt;FROM SASHELP.VTABLE&lt;BR /&gt;WHERE LIBNAME='STPSAMP';&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;DATA RESULTS;&lt;BR /&gt;LENGTH DATASET $32. VARIABLE_EXISTS $3.;&lt;BR /&gt;STOP;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;%MACRO CHECK_VARIABLE(VAR_NAME);&lt;/P&gt;
&lt;P&gt;%LET DS_COUNT=%SYSFUNC(COUNTW(&amp;amp;DATASET_LIST.));&lt;BR /&gt;%DO i=1 %TO &amp;amp;DS_COUNT.;&lt;BR /&gt;%LET DATASET=%SCAN(&amp;amp;DATASET_LIST.,&amp;amp;i.);&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MEMNAME,COUNT(*) INTO : DT_NAME, :VAR_EXISTS&lt;BR /&gt;FROM DICTIONARY.COLUMNS&lt;BR /&gt;WHERE LIBNAME='STPSAMP' AND MEMNAME="&amp;amp;DATASET."&lt;BR /&gt;AND UPPER(NAME)="&amp;amp;VAR_NAME.";&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;DATA _NULL_;&lt;BR /&gt;DATASET=&amp;amp;DATASET.;&lt;BR /&gt;VARIABLE_EXISTS=IFC("&amp;amp;VAR_EXISTS."&amp;gt;0,'YES','NO');&lt;BR /&gt;CALL SYMPUT("DATASET",DATASET);&lt;BR /&gt;CALL SYMPUT("VARIABLE_EXISTS",VARIABLE_EXISTS);&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;DATA RESULTS;&lt;BR /&gt;SET RESULTS;&lt;BR /&gt;DATASET=SYMGET(DATASET);&lt;BR /&gt;VARIABLE_EXISTS=SYMGET(VARIABLE_EXISTS);&lt;BR /&gt;OUTPUT;&lt;BR /&gt;RUN;&lt;BR /&gt;%END;&lt;/P&gt;
&lt;P&gt;%MEND CHECK_VARIABLE;&lt;/P&gt;
&lt;P&gt;%CHECK_VARIABLE(QTR);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in Advance&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 16:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/I-need-to-check-whether-the-variable-is-available-if-yes-means-I/m-p/931284#M44864</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2024-06-07T16:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: I need to check whether the variable is available if yes means I need to save that dataset in ou</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/I-need-to-check-whether-the-variable-is-available-if-yes-means-I/m-p/931302#M44865</link>
      <description>&lt;P&gt;Did you have code that worked before you started writing that macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If by "database" you mean a SAS Library then this would be my take. The example looks for any data set in the SASHELP library (which you should have) looking for a variable named Sex. Important to note: the case of the variable name in the Name variable can be of variable case. So I use UPCASE for a comparison to 'SEX', an equivalent lower case could be used. SAS will return a numeric 1/0 result for any logical comparison. So this selects the largest of all the comparisons of the names for each Memname. 1 indicates it was found, 0 not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I strongly recommend using numeric 1 for "Yes" or "True" or "Present" and 0 for "No", "False","Absent" or similar dichotomous values. There things that just plain easier with the numeric values than the character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
    create table work.want as
    select memname,max(upcase(name)='SEX') as Variable_exists
    from dictionary.columns
    where LIBNAME='SASHELP'
    group by memname
    ;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would have to show an example of how you want your "multiple" , assuming you mean multiple variables, output data set to look.&lt;/P&gt;
&lt;P&gt;For example a minor change to my code would give a count of how many variables are in the set:&lt;/P&gt;
&lt;PRE&gt;proc sql;
    create table work.want as
    select memname,sum(upcase(name)in ('SEX' 'AGE') ) as Variable_exists
    from dictionary.columns
    where LIBNAME='SASHELP'
    group by memname
    ;
quit;&lt;/PRE&gt;
&lt;P&gt;An example of that "numeric 1/0 is easier to work with" for many things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 17:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/I-need-to-check-whether-the-variable-is-available-if-yes-means-I/m-p/931302#M44865</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-07T17:22:32Z</dc:date>
    </item>
  </channel>
</rss>

