<?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 value from a column which is used in different data sources in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/874974#M42907</link>
    <description>&lt;P&gt;That's an extract all of the data sets with the variable and then select from each where the &lt;STRONG&gt;variable&lt;/STRONG&gt; has the value.&lt;/P&gt;
&lt;P&gt;Likely not "simple" code but routine and tedious.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your question is a subset of this one: &lt;A href="https://communities.sas.com/t5/SAS-Programming/Finding-a-value-in-all-datasets-in-all-variables/m-p/315651" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Finding-a-value-in-all-datasets-in-all-variables/m-p/315651&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;In that case the question involved multiple variable names, you know the name of the variable, Code so should be simpler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really want to get used to thinking of variables when thinking of values in data sets. That is different than columns in reports or output documents.&lt;/P&gt;</description>
    <pubDate>Wed, 10 May 2023 14:43:35 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-05-10T14:43:35Z</dc:date>
    <item>
      <title>Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/874954#M42906</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;before I had the problem that I wanted to find out in which data sources a column is used. That worked with following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;Create table found as&lt;/P&gt;&lt;P&gt;Select * FROM dictionary.columns&lt;/P&gt;&lt;P&gt;Where upcase(name) = "MFLL";&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Now i have the value "CM8". This value is used in the column "Code". The column "Code" is used in differenct data sources.&lt;/P&gt;&lt;P&gt;I want to find out all data sources where the value "CM8" appears. Do you have an idea for an simple code? I tried to expand my previous code but I dont get any result.&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 14:10:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/874954#M42906</guid>
      <dc:creator>jozumhannes</dc:creator>
      <dc:date>2023-05-10T14:10:16Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/874974#M42907</link>
      <description>&lt;P&gt;That's an extract all of the data sets with the variable and then select from each where the &lt;STRONG&gt;variable&lt;/STRONG&gt; has the value.&lt;/P&gt;
&lt;P&gt;Likely not "simple" code but routine and tedious.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your question is a subset of this one: &lt;A href="https://communities.sas.com/t5/SAS-Programming/Finding-a-value-in-all-datasets-in-all-variables/m-p/315651" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Finding-a-value-in-all-datasets-in-all-variables/m-p/315651&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;In that case the question involved multiple variable names, you know the name of the variable, Code so should be simpler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really want to get used to thinking of variables when thinking of values in data sets. That is different than columns in reports or output documents.&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 14:43:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/874974#M42907</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-10T14:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875040#M42910</link>
      <description>Im sorry, I didnt get that.</description>
      <pubDate>Wed, 10 May 2023 17:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875040#M42910</guid>
      <dc:creator>jozumhannes</dc:creator>
      <dc:date>2023-05-10T17:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875043#M42911</link>
      <description>&lt;P&gt;This may not be the most elegant way, but you can use PROC SQL and query dictionary.columns to get the names of the datasets that contain the CODE variable (and library name if necessary), and also the number of data sets that have the variable, and write those values to macro variables. Then in a macro you can read each one and look for a specific value in the CODE variable. If the data set contains it, capture the data set name and write it out to a final data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data x1;&lt;BR /&gt;input code $;&lt;BR /&gt;cards;&lt;BR /&gt;AB1&lt;BR /&gt;CD2&lt;BR /&gt;EF3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data x2;&lt;BR /&gt;input code $;&lt;BR /&gt;cards;&lt;BR /&gt;AB2&lt;BR /&gt;CD2&lt;BR /&gt;EF4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data x3;&lt;BR /&gt;input code $;&lt;BR /&gt;cards;&lt;BR /&gt;AB1&lt;BR /&gt;CD3&lt;BR /&gt;EF3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc datasets lib=work;&lt;BR /&gt;delete find final;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select count(memname) into :cnt from dictionary.columns&lt;BR /&gt;Where upcase(name) = "CODE";&lt;BR /&gt;select memname into :dsets1-:dsets%trim(&amp;amp;cnt)&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;Where upcase(name) = "CODE";&lt;BR /&gt;select libname into :lib1-:lib%trim(&amp;amp;cnt)&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;Where upcase(name) = "CODE";&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro v_find;&lt;BR /&gt;%DO i=1 %TO &amp;amp;cnt;&lt;BR /&gt;DATA find;&lt;BR /&gt;set &amp;amp;&amp;amp;lib&amp;amp;i...&amp;amp;&amp;amp;dsets&amp;amp;i;&lt;BR /&gt;if code='CD2' then do;&lt;BR /&gt;dname="&amp;amp;&amp;amp;dsets&amp;amp;i";&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%let dsid=%sysfunc(open(work.find)); &lt;BR /&gt;%let ret=%sysfunc(attrn(&amp;amp;dsid,any)); &lt;BR /&gt;%let rc=%sysfunc(close(&amp;amp;dsid)); &lt;BR /&gt;&lt;BR /&gt;%if &amp;amp;ret GE 1 %then %do;&lt;BR /&gt;proc append base=final (keep=dname) data=find force;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%v_find;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 18:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875043#M42911</guid>
      <dc:creator>jebjur</dc:creator>
      <dc:date>2023-05-10T18:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875068#M42912</link>
      <description>Hi, first of all thank you for helping me!&lt;BR /&gt;I tried this Code but it didnt work.&lt;BR /&gt;I have 3 values as output data.(X1, X2 and X3). I think they are unimportant&lt;BR /&gt;&lt;BR /&gt;As a result I have two tables&lt;BR /&gt;once "Directory" and once a table with "Name", "Member Type", "File Size" and "Last modified". But these are not the data sources</description>
      <pubDate>Wed, 10 May 2023 19:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875068#M42912</guid>
      <dc:creator>jozumhannes</dc:creator>
      <dc:date>2023-05-10T19:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875644#M42953</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should find an output table called work.final with two rows containing the results 'X1' and 'X2' ...&lt;/P&gt;
&lt;P&gt;This is what I get running jebjur's code in SAS on demand. ... and that is correct searching the sample tables for CODE='CD2', is it not?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And being with it: What is wrong with the solution ballardw has cited? ... besides that it uses iml which might be intimidating for a new user.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers fja&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 09:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875644#M42953</guid>
      <dc:creator>fja</dc:creator>
      <dc:date>2023-05-14T09:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875646#M42954</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx(".",libname,memname) into :dslist separated by " "
from dictionary.columns
where upcase(name) = "CODE";
quit;

data want;
length dataset_name $41;
set
  &amp;amp;dslist.
  indsname=dsname
;
where code = "CM8";
dataset_name = dsname;
keep dataset_name;
run;

proc sort data=want nodupkey;
by dataset_name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested.&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 09:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875646#M42954</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-14T09:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875663#M42955</link>
      <description>&lt;P&gt;I did small test and the code works well, if I may, few suggestions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) I would add a dummy dataset to be sure the &amp;amp;dslist. is not null:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _;
  code=" ";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2)&amp;nbsp; since the value is "CM8" I would add condition for type, to avoid selecting botch character and numeric variables,&lt;/P&gt;
&lt;P&gt;3) I would also add selection of maximal length among selected variables,&lt;/P&gt;
&lt;P&gt;4) (optionally) I doubt there will be more 1561 (65534/42) datasets, but "better safe than sorry" so I would go with a macro-array:&amp;nbsp;dslist1,&amp;nbsp;dslist2,...,dslistN&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;data _;
  code=" ";
run;

proc sql noprint;
select 
  catx(".",libname,memname) 
  ,max(length)
into :dslist1-,                             /* make macro-array */
     :len                                   /* get max length */
from dictionary.columns
where upcase(name) = "CODE"
and type="char"                             /* condition for type */
;
%let n=&amp;amp;SQLobs;
quit;

%put &amp;amp;=N.;
%put &amp;amp;=dslist1.;
%put &amp;amp;=dslist2.;
%put dslist&amp;amp;N.=&amp;amp;&amp;amp;dslist&amp;amp;N.;
%put &amp;amp;=len.;

data want;
  length dataset_name $ 41 code $ &amp;amp;len.;
  set
    %macro _(n);
      %do n=1 %to &amp;amp;n.;
        &amp;amp;&amp;amp;dslist&amp;amp;n.
      %end;
    %mend;
    %_(&amp;amp;N.)                                 /* loop over macro array */

    indsname=dsname
  ;
  where code = "CM8";

  dataset_name = dsname;
  keep dataset_name;
run;

proc sort data=want nodupkey;
  by dataset_name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 13:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875663#M42955</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-14T13:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875665#M42956</link>
      <description>&lt;P&gt;If there is a risk of too many datasets, I would create the code to a temporary file and %INCLUDE that.&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 13:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875665#M42956</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-14T13:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Find a value from a column which is used in different data sources</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875667#M42957</link>
      <description>&lt;P&gt;one more "safety vault" came into my mind, also adding:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;&amp;amp;dslist&amp;amp;n(keep=code)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would prevent "type mismatch" for other variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Taking all into account something like this macro wrapper:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro findVarVal(variable, value, type=char);

  %local n len;
  data _1;
    &amp;amp;variable.=" ";
  run;

  data _2;
    &amp;amp;variable.=.;
  run;

  proc sql noprint;
  select 
    catx(".",libname,memname) 
    ,max(length)
  into :dslist1-,
       :len
  from dictionary.columns
  where upcase(name) = %upcase("&amp;amp;variable.")
  and type="&amp;amp;type."
  ;
  %let n=&amp;amp;SQLobs;
  quit;
  /*
  %put &amp;amp;=N.;
  %put &amp;amp;=dslist1.;
  %put &amp;amp;=dslist2.;
  %put dslist&amp;amp;N.=&amp;amp;&amp;amp;dslist&amp;amp;N.;
  %put &amp;amp;=len.;
  */
  data want;
    length dataset_name $ 41 &amp;amp;variable. 
    %if &amp;amp;type.=char %then $; &amp;amp;len.
    ;
    set
        %do n=1 %to &amp;amp;n.;
          &amp;amp;&amp;amp;dslist&amp;amp;n.(keep=&amp;amp;variable.)
        %end;
      indsname=dsname
    ;
    %if &amp;amp;type.=num %then
      %do;
        where &amp;amp;variable. = &amp;amp;value.;
      %end;
    %if &amp;amp;type.=char %then
      %do;
        where &amp;amp;variable. = symget('value');
      %end;

    dataset_name = dsname;
    keep dataset_name;
  run;

  proc sort data=want nodupkey;
    by dataset_name;
  run;
%mend findVarVal;

%findVarVal(code, CM8)


%findVarVal(name, John)


%findVarVal(age, 14, type=num)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(not 100% bullet proof, but good enough)&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 13:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Find-a-value-from-a-column-which-is-used-in-different-data/m-p/875667#M42957</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-14T13:16:15Z</dc:date>
    </item>
  </channel>
</rss>

