<?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: Select columns containing a specific value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926810#M364740</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465691"&gt;@victor1893&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The solution that I'm looking needs to be run on tables with thousands of records and hundreds of columns. Proc transpose is not very computationally efficient in this case.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you will have to use more complicated code.&amp;nbsp; Something like this:&lt;/P&gt;
&lt;P&gt;(NOTE: You could easily generate the code from a list of variable.&amp;nbsp; You could generate the list of variable using PROC CONTENT if needed.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table summary as 
select max( abc=99 ) as abc
     , max( def=99 ) as def
     , max( ghi=99 ) as ghi
     , max( jkl=99 ) as jkl
from have
;
quit;

proc transpose data=summary out=want(where=(col1=1)) ;
  var _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could possibly use a data step and an array (or a couple of arrays)&amp;nbsp; that would have the advantage that you could stop as soon as you found that 99 appeared in every column.&amp;nbsp; If you have more than 5000 numeric variables then increase the size of the temporary array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have end=eof;
  array vars _numeric_;
  array flags[5000] _temporary_ (5000*0);
  length _name_ $32 ;
  do _n_=1 to dim(vars);
    if vars[_n_]=99 then flags[_n_]=1;
  end;
  if eof or sum(of flags[*])=dim(vars) then do;
    do _n_=1 to dim(vars);
      if flags[_n_] then do;
        _name_ = vname(vars[_n_]);
        output;
      end;
    end;
    stop;
  end;
  keep _name_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 02 May 2024 15:45:46 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-05-02T15:45:46Z</dc:date>
    <item>
      <title>Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926790#M364729</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table from which I want to store in a macro variable all columns that contain a specific value. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input abc def ghi jkl 8.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 4 2 3&lt;BR /&gt;6 99 0 8&lt;BR /&gt;3 99 6 5&lt;BR /&gt;2 4 0 99&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I would like to obtain all columns which contain the value 99: def, jkl.&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 14:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926790#M364729</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-05-02T14:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926797#M364732</link>
      <description>&lt;P&gt;Is 99 a value chosen to be larger than any other "real" value that might appear? Why use 99 instead of missing? Or could there be the number 100 in a column?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If 99 is the largest value in the columns, you can use PROC SUMMARY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    var _numeric_;
    output out=_stats_ max=;
run;

proc transpose data=_stats_ out=_stats2_;
run;

proc sql noprint;
select _name_ into :which_cols separated by ' ' from _stats2_ where col1=99;
quit;

%put &amp;amp;=which_cols;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2024 14:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926797#M364732</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-02T14:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926798#M364733</link>
      <description>&lt;P&gt;99 is an arbitrary value given as an example. I am looking for a solution that can work for any given value. The proc summary solution doesn't work unfortunately but I appreciate your effort!&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 14:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926798#M364733</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-05-02T14:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926803#M364735</link>
      <description>&lt;P&gt;Why not first transpose the data?&lt;/P&gt;
&lt;P&gt;First let's fix your data step.&amp;nbsp; It does not make any sense to read exactly 8 bytes to get the value for the last variable on the line, just let SAS read it the same as you did the other three.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input abc def ghi jkl ;
datalines;
1 4 2 3
6 99 0 8
3 99 6 5
2 4 0 99
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you transpose the data then you can use something like WHICHN() (or use WHICHC() if you are searching for character values).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=trans ;
  var _all_;
run;

data want;
  set trans;
  if whichn(99,of col:);
  keep _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then pull that list of variable name into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nliteral(_name_) into :varlist separated by ' '
from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2024 15:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926803#M364735</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-02T15:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926808#M364738</link>
      <description>&lt;P&gt;The solution that I'm looking needs to be run on tables with thousands of records and hundreds of columns. Proc transpose is not very computationally efficient in this case.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 15:30:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926808#M364738</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-05-02T15:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926810#M364740</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465691"&gt;@victor1893&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The solution that I'm looking needs to be run on tables with thousands of records and hundreds of columns. Proc transpose is not very computationally efficient in this case.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you will have to use more complicated code.&amp;nbsp; Something like this:&lt;/P&gt;
&lt;P&gt;(NOTE: You could easily generate the code from a list of variable.&amp;nbsp; You could generate the list of variable using PROC CONTENT if needed.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table summary as 
select max( abc=99 ) as abc
     , max( def=99 ) as def
     , max( ghi=99 ) as ghi
     , max( jkl=99 ) as jkl
from have
;
quit;

proc transpose data=summary out=want(where=(col1=1)) ;
  var _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could possibly use a data step and an array (or a couple of arrays)&amp;nbsp; that would have the advantage that you could stop as soon as you found that 99 appeared in every column.&amp;nbsp; If you have more than 5000 numeric variables then increase the size of the temporary array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have end=eof;
  array vars _numeric_;
  array flags[5000] _temporary_ (5000*0);
  length _name_ $32 ;
  do _n_=1 to dim(vars);
    if vars[_n_]=99 then flags[_n_]=1;
  end;
  if eof or sum(of flags[*])=dim(vars) then do;
    do _n_=1 to dim(vars);
      if flags[_n_] then do;
        _name_ = vname(vars[_n_]);
        output;
      end;
    end;
    stop;
  end;
  keep _name_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2024 15:45:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926810#M364740</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-02T15:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926812#M364741</link>
      <description>&lt;P&gt;The data step solution (although ugly) worked as expected.&amp;nbsp; Thank you very much for the effort!&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 15:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926812#M364741</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-05-02T15:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926814#M364742</link>
      <description>&lt;P&gt;How to generate the PROC SQL method from the variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(obs=0) out=names; var _all_; run;
filename code temp;
data _null_;
  file code;
  set names end=eof;
  length nliteral $64 ;
  nliteral=nliteral(_name_);
  if _n_=1 then put
  'proc sql;'
 /'create table summary as'
 /'select ' @;
  else put
  '     , ' @;
  put 'max( ' nliteral '=99 ) as ' nliteral ;
  if eof then put 'from have;'/'quit;';
run;
%include code ;
proc transpose data=summary out=want(where=(col1=1)) ;
  var _all_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if the actual dataset lives in a remote database (Oracle, Snowflake, Teradata, etc) this has the advantage (perhaps requiring changing the simple boolean test into case when var=99 then 1 else 0 end) of being able to be pushed into a remote data to execute thus massively decreasing the amount of data that needs to move from the remote database into the SAS server.&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 16:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926814#M364742</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-02T16:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926815#M364743</link>
      <description>&lt;P&gt;Appreciate the tip, but I believe this is a bit too complex for the average sas user in my organization, so I don't want to go deeper than this. Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 16:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926815#M364743</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-05-02T16:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Select columns containing a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926868#M364775</link>
      <description>&lt;P&gt;The most convenient and succinct way is using PROC IML.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input abc def ghi jkl 8.;
datalines;
1 4 2 3
6 99 0 8
3 99 6 5
2 4 0 99
;
run;

proc iml;
use have;
read all var _all_ into x[c=vname];
close;
idx=loc((x=99)[&amp;lt;&amp;gt;,]);
want=vname[idx];
print want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1714712900107.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96142iFA8D03A69E7C4B15/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1714712900107.png" alt="Ksharp_0-1714712900107.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2024 05:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-columns-containing-a-specific-value/m-p/926868#M364775</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-05-03T05:08:37Z</dc:date>
    </item>
  </channel>
</rss>

