<?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 How to find a dataset with a variable equal to certain value in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-find-a-dataset-with-a-variable-equal-to-certain-value/m-p/935709#M42038</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I have 50 datasets in a directory, all with the same format, for example,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset1:&lt;BR /&gt;ID x y&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; a&amp;nbsp; apple&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;b&amp;nbsp; banana&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp;c&amp;nbsp; orange&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset2-dataset50 all have the same variables, ID, x, and y. How do I find datasets that have x = c? Let's assume out of all 50 datasets, only dataset1 and dataset 15 have records with x = c. Is there a way to look through all datasets and check for a value of particular variable and return the name of the datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Sat, 13 Jul 2024 17:00:02 GMT</pubDate>
    <dc:creator>cosmid</dc:creator>
    <dc:date>2024-07-13T17:00:02Z</dc:date>
    <item>
      <title>How to find a dataset with a variable equal to certain value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-find-a-dataset-with-a-variable-equal-to-certain-value/m-p/935709#M42038</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I have 50 datasets in a directory, all with the same format, for example,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset1:&lt;BR /&gt;ID x y&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; a&amp;nbsp; apple&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;b&amp;nbsp; banana&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp;c&amp;nbsp; orange&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset2-dataset50 all have the same variables, ID, x, and y. How do I find datasets that have x = c? Let's assume out of all 50 datasets, only dataset1 and dataset 15 have records with x = c. Is there a way to look through all datasets and check for a value of particular variable and return the name of the datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jul 2024 17:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-find-a-dataset-with-a-variable-equal-to-certain-value/m-p/935709#M42038</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2024-07-13T17:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a dataset with a variable equal to certain value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-find-a-dataset-with-a-variable-equal-to-certain-value/m-p/935714#M42039</link>
      <description>&lt;P&gt;You will have to read the data to see if the values are there.&lt;/P&gt;
&lt;P&gt;If the datasets all have the same variables then you can read them all in one data step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the INDSNAME= option of the SET statement to know which dataset the current observation comes from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So use a data step like this to read all of the data and keep the names of the datasets that have observations that match your criteria.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length dsname indsname $41 ;
  set dataset1-dataset50 indsname=indsname ;
  where x='c';
  dsname=indsname;
  if dsname ne lag(dsname) then output;
  keep dsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The only trouble you might have is getting the list of names into the SET statement if the names are not as simple as that.&amp;nbsp; In that case you might want to get the list into a macro variable.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if you want all of the datasets in the libref MYLIB that have a variable named X then the code might look like this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct catx('.',libname,memname)
    into :memlist separated by ' '
 from dictionary.columns 
 where libname='MYLIB' and upcase(name)='X'
;
quit;
data want;
  length dsname indsname $41 ;
  set &amp;amp;memlist indsname=indsname ;
  where x='c';
  dsname=indsname;
  if dsname ne lag(dsname) then output;
  keep dsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If the datasets might have conflicts for some other variable types (Variables other than X,&amp;nbsp; for example say Y is character in DATASET1 and numeric in DATASET2) then you will want to add a KEEP= dataset option to each dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jul 2024 19:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-find-a-dataset-with-a-variable-equal-to-certain-value/m-p/935714#M42039</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-13T19:01:41Z</dc:date>
    </item>
  </channel>
</rss>

