<?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: Find a specific value in a specific variable in all SAS datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946375#M370619</link>
    <description>&lt;P&gt;Can you set some limits on this problem?&lt;/P&gt;
&lt;P&gt;Do all of the datasets have the same variables (defined the same way)&amp;nbsp; so that you could use a single data step to read them all?&amp;nbsp;Do all of the datasets of interest use a common prefix (and only the dataset of interest using that prefix)?&amp;nbsp; If so then perhaps something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set QPPCT.prefix: indsname=dsname;
  where TABNC in ('123');
  if dsname ne lag(dsname);
  dataset = scan(dsname,-1,'.');
  keep dataset;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise get a list of the dataset that have the TABNC variable and use that to write the list of dataset names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=QPPCT._all_ noprint out=contents; run;
proc sql ;
  select catx('.',libname,memname) into :dslist separated by ' '
  from contents
  where upcase(name)='TABNK')
;
quit;
data want;
  set &amp;amp;dslist indsname=dsname;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Oct 2024 21:49:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-04T21:49:53Z</dc:date>
    <item>
      <title>Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946339#M370614</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am referencing to a 2016 article "&lt;A href="https://communities.sas.com/t5/SAS-Programming/Finding-a-value-in-all-datasets-in-all-variables/td-p/315651" target="_blank" rel="noopener"&gt;Finding a value in all datasets in all variables&lt;/A&gt;."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not use PROC IML but we are licensed for this product.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal is to find a specific value '024683579' in variable TABNC in library QPPCT.&amp;nbsp; The library QPPCT has over 9K datasets with nearly all of them having the same variable TABNC.&amp;nbsp; I need the name of the dataset(s) found and the creation date.&amp;nbsp; The purpose is to find a specific (or multiple) datasets with this value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can the 2016 solution be adapted for this use or is there a better way to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Curtis&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 19:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946339#M370614</guid>
      <dc:creator>CurtisER</dc:creator>
      <dc:date>2024-10-04T19:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946341#M370615</link>
      <description>&lt;P&gt;The code would work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Better way" might depend on the size of those 9K data sets.&lt;/P&gt;
&lt;P&gt;One way would be at use a data set with SET statement to stack all of the data sets with the INDSNAME option to add the data set name as a variable. But this may only be viable if your data sets have been very consistent about setting variable types the same for same-named variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By "creation date" do you mean of the data set as would be stored in dictionary.tables or something else?&lt;/P&gt;
&lt;P&gt;If the creation date wanted is the one in dictionary.tables (the view Sashelp.vtable ) then match and add using the data set name kept in the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something along the lines of:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table &amp;lt;somelib&amp;gt;.tabnc as
   select libname, memname, catx('.',libname,memname) as dsn
   from dictionary.columns
   where libname='QPPCT' and upcase(name)='TABCN';
quit;

/* write code to build the data set with values*/

data _null_;
   set &amp;lt;somelib&amp;gt;.tabnc end=lastone;;
   file "&amp;lt;path&amp;gt;\get_tabnc.sas";
   if _n_=1 then do;
      put "data &amp;lt;somelib&amp;gt;.tabnc_values;";
      put "set ";
   end;
   /* specify the data set names and keep only the TABCN variable */
   put dsn +1 "(keep=tabnc) " ;
   if lastone then do;
      /* add the Indsname option and end the SET statement*/
      put "indsname=dsn ;"
      put "Where tabnc =  '024683579' ;";
      /* keep the temporary variable DSN from the INDSNAME into
         another variable
      */
      put "source=dsn ;";
      /* add the run for the data step*/
      put "Run ;";
      
   end; 
run;

/* execute the data step created above */
%include "&amp;lt;path&amp;gt;\get_tabnc.sas";

/* get createion dates */
Proc sql;
    create table &amp;lt;somelib&amp;gt;.tabcn_created as
    select a.dsn,b.crdate
    from (select distinct dsn from  &amp;lt;somelib&amp;gt;.tabnc_values as a
         left join
         (select * from dictionary.tables 
          where libname='QPPCT' )as b
          on a.dsn = catx('.',b.libname,b.memname)
    ;
quit;

/* the crdate should be easy to merge back to the &amp;lt;somelib&amp;gt;.tabnc_values
  on the value of dsn
*/
&lt;/PRE&gt;
&lt;P&gt;This has a minor advantage of the data step that selects the values is a separate program file for review.&lt;/P&gt;
&lt;P&gt;Pick your values for the &amp;lt;somelib&amp;gt; library to store the data set and the &amp;lt;path&amp;gt; to store the generated data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously untested as I have none of&amp;nbsp; your libraries or data sets.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 20:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946341#M370615</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-04T20:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946342#M370616</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;By "creation date" do you mean of the data set as would be stored in dictionary.tables or something else?&lt;/P&gt;&lt;P&gt;If the creation date wanted is the one in dictionary.tables (the view Sashelp.vtable ) then match and add using the data set name kept in the first.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This is merely for reporting to folks who wants to know the age (but that really isn't important in this case).&amp;nbsp; My apologies.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable TABNC can be thought of as a key ID variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will look into your suggestions, thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 19:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946342#M370616</guid>
      <dc:creator>CurtisER</dc:creator>
      <dc:date>2024-10-04T19:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946375#M370619</link>
      <description>&lt;P&gt;Can you set some limits on this problem?&lt;/P&gt;
&lt;P&gt;Do all of the datasets have the same variables (defined the same way)&amp;nbsp; so that you could use a single data step to read them all?&amp;nbsp;Do all of the datasets of interest use a common prefix (and only the dataset of interest using that prefix)?&amp;nbsp; If so then perhaps something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set QPPCT.prefix: indsname=dsname;
  where TABNC in ('123');
  if dsname ne lag(dsname);
  dataset = scan(dsname,-1,'.');
  keep dataset;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise get a list of the dataset that have the TABNC variable and use that to write the list of dataset names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=QPPCT._all_ noprint out=contents; run;
proc sql ;
  select catx('.',libname,memname) into :dslist separated by ' '
  from contents
  where upcase(name)='TABNK')
;
quit;
data want;
  set &amp;amp;dslist indsname=dsname;
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 21:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946375#M370619</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-04T21:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946402#M370627</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data a;
input a : $40. b c;
cards;
Thisaccount,024683579 1234567891 1234567891
wwrew 232442 343
;
run;
data b;
input TABNC  : $40. bb xxxx : $20.;
cards;
qweqwe 233 2323
Thisaccount,024683579 1234567891 1234567891
;
run;
data c;
input c : $40. bbbbbb;
cards;
Thisaccount 1234567891
Thisaccount,1234567891 2324
;
run;


%let libname=work;  *the name of library you want to search;

proc iml;
dsn="&amp;amp;libname.."+datasets("&amp;amp;libname.");
do i=1 to nrow(dsn);
use (dsn[i]);
 read all var _char_ into x[c=vnames];
 flag=(find(x,'024683579')^=0);
 do j=1 to nrow(flag);
  if any(flag[j,]=1) then do;
    loc=loc(flag[j,]);
    temp=vnames[loc];
    vname=vname//temp;
    obs=obs//repeat(j,nrow(temp));
    table=table//repeat(dsn[i],nrow(temp));
  end;
 end;
/* 
 read all var _num_ into x[c=vnames];
 flag=(x=1234567891);
 do j=1 to nrow(flag);
  if any(flag[j,]=1) then do; 
    loc=loc(flag[j,]);
    temp=vnames[loc]; 
    vname=vname//temp;
    obs=obs//repeat(j,nrow(temp));
    table=table//repeat(dsn[i],nrow(temp));
  end;
 end;
*/
close (dsn[i]);
end;

create want var {table vname obs};
append;
close;
quit;

proc contents data=&amp;amp;libname.._all_ out=out noprint;
run;
proc sql;
create table want3 as
select distinct a.*,CRDATE,MODATE
 from (select * from want where upcase(vname)='TABNC') as a left join out as b
  on b.MEMNAME = upcase(scan(a.TABLE,-1,'.'));
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2024 01:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946402#M370627</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-07T01:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946411#M370633</link>
      <description>&lt;P&gt;If you really need only the names of datasets with at least one instance of the value, you can build a SET statement with all the candidate datasets, but use OBS=1 to tell SAS to stop processing a dataset once an instance has been encountered.&amp;nbsp; Looking for variable X=2001 in the code below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data xxx;  /*Four instances of x=2001 */
  do x=1111 to 2222;
    output;

  end;
  x=2001;  output; output; output;
run;
data yyy;  /*One instance of x=2001 */
  do x=2222 to 1111 by -1;
    output;
  end;
run;
data zzz;  /*No variable X */
  do y= 1111 to 2222;
    output;
  end;
run;
proc sql noprint;
  create table need as
    select libname, memname, catx('.',libname,memname) as dsn
    from dictionary.columns
   where libname='WORK' and upcase(name)='X'
   ;
  create table candidates as 
    select n.*,     t.crdate
    from need as N left join dictionary.tables as T
    on n.libname=t.libname and n.memname=t.memname;
  drop table need;
  select distinct  catx(' ',dsn,"(keep=x where=(x=2001) obs=1)")
    into :dsn_list separated by ' ' 
     from candidates
  ;
quit;

%put &amp;amp;=dsn_list ;
data want;
  set &amp;amp;dsn_list
      indsname=indsn ;
  if _n_=1 then do;
    if 0 then set candidates;
    declare hash h (dataset:'candidates');
      h.definekey('dsn');
      h.definedata(all:'Y');
      h.definedone();
  end;
  h.find(key:indsn);
  put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Oct 2024 22:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946411#M370633</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-10-05T22:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find a specific value in a specific variable in all SAS datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946537#M370668</link>
      <description>&lt;P&gt;I hope you all a nice weekend!&amp;nbsp; Thank you everyone!&amp;nbsp; I'm testing against some of your sample code and I'll update back with the solution I've found.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Curtis&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-a-specific-value-in-a-specific-variable-in-all-SAS-datasets/m-p/946537#M370668</guid>
      <dc:creator>CurtisER</dc:creator>
      <dc:date>2024-10-07T14:10:50Z</dc:date>
    </item>
  </channel>
</rss>

