<?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 variables having a particular string as part of its name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571948#M161372</link>
    <description>&lt;P&gt;Another way of achieving the goal in a single step is using CALL VNEXT instead of the dual arrays (as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;on SAS-L) coupled with CALL EXECUTE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                     
  input ID :$3. X_1 X_2 X_3 Y_1 Y_2 W_3  :$1. ; 
  cards ;                                       
ID1 1 2 3 4 5 6 C                               
ID2 2 3 4 5 6 7 D                               
run ;                                           
                                                
data _null_ ;                                   
  set have (obs = 1) ;                          
  length _vn $ 32 ;                              
  call execute ("data want; set have(keep=") ;  
  do until (cmiss (_vn)) ;                      
    call vnext (_vn) ;                          
    if find (_vn, "3") then call execute (_vn) ;
  end ;                                         
  call execute ("); run;") ;                    
run ;                                           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jul 2019 03:42:07 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-07-09T03:42:07Z</dc:date>
    <item>
      <title>select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571634#M161261</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set called tbl1.&lt;/P&gt;
&lt;P&gt;I want to&amp;nbsp;select variables having a particular string as part of its name.&lt;/P&gt;
&lt;P&gt;for example: I want to select all variables that contain "3" (so need to select&amp;nbsp; X_3 ,W_3)&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl1;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3;
cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8 
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 07:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571634#M161261</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T07:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571635#M161262</link>
      <description>&lt;PRE&gt;Data have;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3;
cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8 
;
run;

data _null_;
   length List $ 32767;
   set have;
   array v X_1 -- W_3;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if substr(vname(v[i]),3,1) = '3' then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List', List);
   end;
stop;   
run;

%put &amp;amp;List;

data want;
   set have;
keep ID &amp;amp;List;   
run;

proc print data = want;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 08:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571635#M161262</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-07T08:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571636#M161263</link>
      <description>&lt;P&gt;Great and thank you!&lt;/P&gt;
&lt;P&gt;Can i ask if there is another way to write&amp;nbsp; the statement&amp;nbsp; &amp;nbsp;&amp;nbsp; array v&amp;nbsp; &amp;nbsp; X_1 -- W_3;&lt;/P&gt;
&lt;P&gt;Instead of writing&amp;nbsp; &amp;nbsp;X_1 -- W_3&amp;nbsp; ,Can I write _ALL_??&lt;/P&gt;
&lt;P&gt;I want that the array will contain all columns without specifying their names&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 08:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571636#M161263</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T08:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571637#M161264</link>
      <description>&lt;P&gt;_ALL_ will take all variables including ID into the Array. Please check it by testing for your application.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 08:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571637#M161264</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-07T08:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571638#M161265</link>
      <description>&lt;P&gt;When I run with _ALL_ I receive an error&lt;/P&gt;
&lt;P&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;BR /&gt;ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3;
cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8 
;
run;

data cc;
   length List $ 32767;
   set have;
   array  v   _ALL_;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if substr(vname(v[i]),3,1) = '3' then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List', List);
   end;
stop;   
run;
%put &amp;amp;List;


data want;
set have;
array vn &amp;amp;List;
keep ID &amp;amp;List;   
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 09:56:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571638#M161265</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T09:56:57Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571639#M161266</link>
      <description>&lt;P&gt;I found solution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3;
cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8 
;
run;

data bbb;
   length List $ 32767;
   set have;
   array  v   _numeric_;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if substr(vname(v[i]),3,1) = '3' then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List1', List);
   end;
stop;   
run;
%put &amp;amp;List1;


data ccc;
   length List $ 32767;
   set have;
   array  v   _character_;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if substr(vname(v[i]),3,1) = '3' then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List2', List);
   end;
stop;   
run;
%put &amp;amp;List2;



data want;
set have;
array vn &amp;amp;List;
keep ID &amp;amp;List1 &amp;amp;List2;   
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 09:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571639#M161266</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T09:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571640#M161267</link>
      <description>&lt;P&gt;What is the purpose of the statement&amp;nbsp; "array vn &amp;amp;List;"?&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 10:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571640#M161267</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T10:00:46Z</dc:date>
    </item>
    <item>
      <title>select variables contain  a particular string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571641#M161274</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is wrong with the code below.&lt;/P&gt;
&lt;P&gt;I want to keep names of columns that contain 3.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3 RRRRR_3 _3T;
cards;
1 2 3 4 5 6 7 9 5
2 3 4 5 6 7 8 8 4 
;
run;

%let field=3;

data bbb;
   length List $ 32767;
   set have;
   array  v   _numeric_;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if vname(v[i])) LIKE "%&amp;amp;field.%" then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List1', List);
   end;
stop;   
run;
%put &amp;amp;List1;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 10:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571641#M161274</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T10:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: select variables contain  a particular string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571643#M161275</link>
      <description>&lt;P&gt;What do you mean by "&lt;SPAN&gt;keep names of columns that contain 3"? Do you want to keep those variables only and drop all other variables?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If so, then do something like this&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select name into :vars separated by ' '
   from dictionary.columns
   where libname='WORK' &amp;amp; memname='HAVE' &amp;amp; find(name, '3') ne 0;
quit;

data want;
   set have;
   keep &amp;amp;vars.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 10:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571643#M161275</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-07T10:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571644#M161271</link>
      <description>&lt;P&gt;Yes, the array statement is not required. I realized it and edited my post in the next minute. You are using the first post. Also, the length of the variables are supposed to be of length not less than 3. Since, ID has a length of 2 and so when SUBSTR() function is used it looks for the third character but it doen't have it. So you got error messages.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 10:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571644#M161271</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-07T10:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: select variables contain  a particular string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571650#M161276</link>
      <description>&lt;P&gt;I merged your two posts concerning the same issue.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jul 2019 11:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571650#M161276</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-07T11:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: select variables contain a particular string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571652#M161278</link>
      <description>Yes. Keep only  variables that contain  3 in their names&lt;BR /&gt;</description>
      <pubDate>Sun, 07 Jul 2019 11:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571652#M161278</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-07T11:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571653#M161279</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl1;
input ID x_1 X_2 X_3 Y_1 Y_2 W_3;
cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8 
;
run;
proc transpose data=tbl1(obs=0) out=temp;
var _all_;
run;
proc sql noprint;
select _name_ into : names separated by ' '
 from temp
  where _name_ contains '3';
quit;
data want;
 set tbl1;
 keep id &amp;amp;names;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 11:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571653#M161279</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-07T11:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: select variables contain  a particular string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571656#M161280</link>
      <description>&lt;P&gt;ID is numeric variable as other variables. But length of Variable ID is 2 whereas others have a length of 3 or more. Change ID as ID1 so that it has a length of 3.&lt;/P&gt;
&lt;PRE&gt;input ID1 x_1 X_2 X_3 Y_1 Y_2 W_3 RRRRR_3 _3T;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also change the statement as:&lt;/P&gt;
&lt;PRE&gt;         *if vname(v[i]) LIKE "%&amp;amp;field.%" then List = catx(' ',List, vname(v[i]));
         if substr(vname(v[i]),3,1) = &amp;amp;field then List = catx(' ',List, vname(v[i]));&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jul 2019 14:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571656#M161280</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-07T14:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571683#M161290</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;1. In general, you can have both numeric and character variables, the names of both having to be accounted for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Using the dynamic properties of the hash object, it can be done in a single step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                     
  input ID :$3. X_1 X_2 X_3 Y_1 Y_2 W_3 :$1. ;                  
  cards ;                                                       
ID1 1 2 3 4 5 6 C                                               
ID2 2 3 4 5 6 7 D                                               
run ;                                                           
                                                                
data _null_ ;                                                   
  if _n_ = 1 then do ;                                          
    dcl hash h (ordered:"A") ;                                  
    h.definekey  ("_n_") ;                                      
    h.definedata ("_n_") ;                                      
    if 0 then set have ;                                        
    array nn _numeric_ ;                                        
    array cc _char_ ;                                           
    do over nn ;                                                
      if find (vname (nn), "3") then h.definedata (vname (nn)) ;
    end ;                                                       
    do over cc ;                                                
      if find (vname (cc), "3") then h.definedata (vname (cc)) ;
    end ;                                                       
    h.definedone() ;                                            
  end ;                                                         
  set have end = z ;                                            
  h.add() ;                                                     
  if z then h.output (dataset:"want (drop = _:)") ;             
run ;                                                           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This way, if none of the variable names contain "3", the output data set will have no variables (that's why _N_ is added as a data variable and dropped in the output).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2019 02:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571683#M161290</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-08T02:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: select variables having a particular string as part of its name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571948#M161372</link>
      <description>&lt;P&gt;Another way of achieving the goal in a single step is using CALL VNEXT instead of the dual arrays (as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;on SAS-L) coupled with CALL EXECUTE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                     
  input ID :$3. X_1 X_2 X_3 Y_1 Y_2 W_3  :$1. ; 
  cards ;                                       
ID1 1 2 3 4 5 6 C                               
ID2 2 3 4 5 6 7 D                               
run ;                                           
                                                
data _null_ ;                                   
  set have (obs = 1) ;                          
  length _vn $ 32 ;                              
  call execute ("data want; set have(keep=") ;  
  do until (cmiss (_vn)) ;                      
    call vnext (_vn) ;                          
    if find (_vn, "3") then call execute (_vn) ;
  end ;                                         
  call execute ("); run;") ;                    
run ;                                           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2019 03:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variables-having-a-particular-string-as-part-of-its-name/m-p/571948#M161372</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-09T03:42:07Z</dc:date>
    </item>
  </channel>
</rss>

