<?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: Generating distinct list of variables of which can have a specific string value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778811#M247952</link>
    <description>This is excellent, thank you very much!</description>
    <pubDate>Fri, 05 Nov 2021 15:41:28 GMT</pubDate>
    <dc:creator>stayhydrated</dc:creator>
    <dc:date>2021-11-05T15:41:28Z</dc:date>
    <item>
      <title>Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778652#M247878</link>
      <description>&lt;P&gt;Summary: I have a very large table (more than 1000 columns) where I need to find (create a list that I can copy from) the variable names that have values that contain the string "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have sample code I wrote and some sample data to help:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input cust_id lorem $ ipsum $ dolor $ sit $ amet $ consectetur $ adipiscing $ elit $ sed $ do $ eiusmod $ tempor $
;
datalines;
1 XXXX KW23 RW16 BA62 KR39 HO32 FX55 VT44 RG81 XXXX SI10 ZK27
1 WW10 ST89 RT44 YQ20 FD70 ZB99 MF40 TV38 HE86 XXXX MU18 OC98
1 IQ59 VM72 XXXX FI95 AT96 GU59 JO89 AW21 FV34 XXXX TU46 TK69
2 IX95 KV92 NU30 BZ39 YS62 VU51 RW99 YX60 AG35 XXXX CM70 BV53
2 XXXX FS77 QI34 NT73 CW86 ED83 HU82 PY22 WS60 XXXX QM63 AN29
3 UP55 RU88 DO14 YW45 OR14 KH75 RA76 MZ65 FD65 XXXX BR51 TE67
4 VX45 NP14 II57 VR57 WY25 FH58 EZ58 XXXX BJ11 XXXX TH83 FY17
4 UJ73 HE29 GV27 FT69 RS17 MK32 CE47 ZJ30 KK48 XXXX KD69 XXXX
4 ZE25 UM46 XXXX UC55 XX99 YS61 YJ23 IM52 ZH76 XXXX MO29 XXXX
5 EA96 AY41 DO99 US92 VC61 HV97 ZR44 CF46 YN99 HC55 HC72 XXXX
;
run; 
quit; 

data have_char; 
	set have (KEEP=_CHARACTER_); 
run; 

proc contents data = have_char NOPRINT OUT=VBLS (KEEP=NAME VARNUM); run; 
proc sort data = VBLS; by VARNUM; run; 

PROC SQL NOPRINT;
SELECT DISTINCT NAME
INTO :var_list separated by " "
FROM VBLS;
QUIT; 

data want_if_it_worked (keep=VARS_WITH_XXXX);
	set have;  
	array val{12} &amp;amp;var_list.; 
	VARS_WITH_XXXX=vname(val(whichc("XXXX",of val(*))));
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the table I am getting with the above code that I &lt;STRONG&gt;&lt;EM&gt;do not&lt;/EM&gt;&lt;/STRONG&gt; want:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stayhydrated_0-1636075447059.png" style="width: 165px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65424iDFF5E07D3D5AA5B2/image-dimensions/165x301?v=v2" width="165" height="301" role="button" title="stayhydrated_0-1636075447059.png" alt="stayhydrated_0-1636075447059.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the table I &lt;STRONG&gt;want&lt;/STRONG&gt; to output from the "&lt;STRONG&gt;have&lt;/STRONG&gt;" data:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stayhydrated_1-1636075532043.png" style="width: 277px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65425iE1DE3B9D7C387D5A/image-dimensions/277x279?v=v2" width="277" height="279" role="button" title="stayhydrated_1-1636075532043.png" alt="stayhydrated_1-1636075532043.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thanks for the help!&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Nov 2021 01:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778652#M247878</guid>
      <dc:creator>stayhydrated</dc:creator>
      <dc:date>2021-11-05T01:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778659#M247882</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  set HAVE;
  array C[*] _CHARACTER_; 
  if whichc('XXXX',of C[*]) then
    FIRSTVAR_WITH_XXX=vname(C[whichc('XXXX',of C[*])]);
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;FIRSTVAR_WITH_XXX&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;lorem&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;do&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;dolor&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;do&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;lorem&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;do&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;elit&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;do&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;dolor&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;tempor&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Nov 2021 04:40:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778659#M247882</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-11-05T04:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778703#M247904</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cust_id lorem $ ipsum $ dolor $ sit $ amet $ consectetur $ adipiscing $ elit $ sed $ do $ eiusmod $ tempor $
;
datalines;
1 XXXX KW23 RW16 BA62 KR39 HO32 FX55 VT44 RG81 XXXX SI10 ZK27
1 WW10 ST89 RT44 YQ20 FD70 ZB99 MF40 TV38 HE86 XXXX MU18 OC98
1 IQ59 VM72 XXXX FI95 AT96 GU59 JO89 AW21 FV34 XXXX TU46 TK69
2 IX95 KV92 NU30 BZ39 YS62 VU51 RW99 YX60 AG35 XXXX CM70 BV53
2 XXXX FS77 QI34 NT73 CW86 ED83 HU82 PY22 WS60 XXXX QM63 AN29
3 UP55 RU88 DO14 YW45 OR14 KH75 RA76 MZ65 FD65 XXXX BR51 TE67
4 VX45 NP14 II57 VR57 WY25 FH58 EZ58 XXXX BJ11 XXXX TH83 FY17
4 UJ73 HE29 GV27 FT69 RS17 MK32 CE47 ZJ30 KK48 XXXX KD69 XXXX
4 ZE25 UM46 XXXX UC55 XX99 YS61 YJ23 IM52 ZH76 XXXX MO29 XXXX
5 EA96 AY41 DO99 US92 VC61 HV97 ZR44 CF46 YN99 HC55 HC72 XXXX
;
run;

proc transpose data=have(obs=0) out=vname;
var _character_;
run;

data _null_;
 set vname end=last;
 if _n_=1 then call execute('proc sql;create table temp as select ');
 call execute(catx(' ','max(upcase(',_name_,')="XXXX") as ',_name_));
 if not last then call execute(',');
  else call execute('from have ;quit;');
run;

proc transpose data=temp out=want(where=(col1=1)) name=vars_with_xxxx;
var _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Nov 2021 11:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778703#M247904</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-05T11:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778764#M247932</link>
      <description>&lt;P&gt;A slightly different twist with additional information.&amp;nbsp; Once you have read in the data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array xx {*} _character_;
   do _n_ = 1 to dim(xx);
      if xx{_n_} = 'XXXX' then do;
         xxxx_name = vname(xx{_n_});
         output;
      end;
   end;
   keep xxxx_name;
run;
proc freq data=want;
   tables xxxx_name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This way, you not only get the list of variables names, you also get a count of how many times each name contains XXXX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Nov 2021 13:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778764#M247932</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-11-05T13:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778807#M247948</link>
      <description>This could work in a pinch - I would have to select the distinct values (which is easy enough). Nevertheless this is still very helpful and I can use this in other times - thank you!</description>
      <pubDate>Fri, 05 Nov 2021 15:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778807#M247948</guid>
      <dc:creator>stayhydrated</dc:creator>
      <dc:date>2021-11-05T15:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778809#M247950</link>
      <description>I really like this slightly different twist! It will be helpful building my skills. Thank you so much!</description>
      <pubDate>Fri, 05 Nov 2021 15:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778809#M247950</guid>
      <dc:creator>stayhydrated</dc:creator>
      <dc:date>2021-11-05T15:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Generating distinct list of variables of which can have a specific string value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778811#M247952</link>
      <description>This is excellent, thank you very much!</description>
      <pubDate>Fri, 05 Nov 2021 15:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-distinct-list-of-variables-of-which-can-have-a/m-p/778811#M247952</guid>
      <dc:creator>stayhydrated</dc:creator>
      <dc:date>2021-11-05T15:41:28Z</dc:date>
    </item>
  </channel>
</rss>

