<?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 How to simplify/shorten this Code? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913005#M359874</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to shorten this code, but couldn't find a working solution yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data Dataset_new;&lt;BR /&gt;   set Dataset_1 Dataset_2 Dataset_3;&lt;BR /&gt;   if whichc('Y1', of A1-A100, C, F) or&lt;BR /&gt;                 ...&lt;BR /&gt;   whichc('Y100', of A1-A100, C, F) or&lt;BR /&gt;   whichc('X', of A1-A100, C, F);&lt;BR /&gt;   run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to extract observations, which have ONE OF the listed VALUES (Y1-Y100,&amp;nbsp; X) in ONE OF the listed VARIABLES (A1-A100, C, F).&lt;/P&gt;&lt;P&gt;I wonder, if it is possible to sum up all the values in one statement (for example in one "WHICHC"-statement) as they all refer to the same set of variables.&lt;/P&gt;&lt;P&gt;Listing multiple values within one "WHICHC"-statement didn't work, no matter, how I tried to separate or combine them (for example by comma, space, "OR"-Operator etc.)&lt;/P&gt;&lt;P&gt;I also tried the "IF FIND(CAT())" combination instead of "IF WHICHC()" but listing multiple values in one statement didn't work there either.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inserting an IN()-function and list the values therein, was also not successful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Often I didn't receive any error messages, but the extracted observations were totally wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance,&lt;/P&gt;&lt;P&gt;Curt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jan 2024 16:51:30 GMT</pubDate>
    <dc:creator>Curt</dc:creator>
    <dc:date>2024-01-25T16:51:30Z</dc:date>
    <item>
      <title>How to simplify/shorten this Code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913005#M359874</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to shorten this code, but couldn't find a working solution yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data Dataset_new;&lt;BR /&gt;   set Dataset_1 Dataset_2 Dataset_3;&lt;BR /&gt;   if whichc('Y1', of A1-A100, C, F) or&lt;BR /&gt;                 ...&lt;BR /&gt;   whichc('Y100', of A1-A100, C, F) or&lt;BR /&gt;   whichc('X', of A1-A100, C, F);&lt;BR /&gt;   run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to extract observations, which have ONE OF the listed VALUES (Y1-Y100,&amp;nbsp; X) in ONE OF the listed VARIABLES (A1-A100, C, F).&lt;/P&gt;&lt;P&gt;I wonder, if it is possible to sum up all the values in one statement (for example in one "WHICHC"-statement) as they all refer to the same set of variables.&lt;/P&gt;&lt;P&gt;Listing multiple values within one "WHICHC"-statement didn't work, no matter, how I tried to separate or combine them (for example by comma, space, "OR"-Operator etc.)&lt;/P&gt;&lt;P&gt;I also tried the "IF FIND(CAT())" combination instead of "IF WHICHC()" but listing multiple values in one statement didn't work there either.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inserting an IN()-function and list the values therein, was also not successful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Often I didn't receive any error messages, but the extracted observations were totally wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance,&lt;/P&gt;&lt;P&gt;Curt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 16:51:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913005#M359874</guid>
      <dc:creator>Curt</dc:creator>
      <dc:date>2024-01-25T16:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify/shorten this Code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913012#M359878</link>
      <description>&lt;P&gt;One always suspects data structure but here is one approach that works. Not 'multiple values' but placing them into a structure that shortens the code. I only provide 5 variables as that is all that is needed to show the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input (a1-a5) ( $);
datalines;
y1 . . . .
b b b b b
b a c d y3
d d d y2 y5
;

data want;
   set have;
   array a (*) a1-a5;
   array t (5) $ 2 _temporary_ ('y1','y2','y3','y4','y5');
   do i= 1 to dim(t);
      if whichc(t[i],of a(*))&amp;gt;0 then do;
         output;
         leave;
      end;
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;The T array would would require a definition for the length of the values large enough to hold the longest value. The _temporary_ definition means the variables are not written to the data set and stay the same as the initial set values unless you change them. If your actual values for y are sequential as shown you could load the array or replace it with a loop building the text of the search items. But real world data is seldom that nice so the temporary array should work though it will be long statement.&lt;/P&gt;
&lt;P&gt;The "trick" is instead of using subsetting IF is the explicit Output when a condition is true coupled with the LEAVE instruction, again conditional, to exit the Do loop early.&lt;/P&gt;
&lt;P&gt;The A array would have all the variables you want to search in so could include your C and D or other variables. I'm just too lazy to make complicated data to demonstrate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do include one example line with two values that are in the search for list to demonstrate only one gets written to the output set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a couple of asides: If you know that some of your values are more likely to occur than others then placing them first in the temporary array will improve efficiency of the code.&lt;/P&gt;
&lt;P&gt;Also, if you wanted to indicate which value was considered as the match then you could a line like:&lt;/P&gt;
&lt;PRE&gt;Found = t[i];&lt;/PRE&gt;
&lt;P&gt;just before the Output statement; Define the length of Found to hold any expected value.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 18:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913012#M359878</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-25T18:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify/shorten this Code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913018#M359880</link>
      <description>&lt;P&gt;A further (but only minor) simplification of &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884" target="_blank" rel="noopener"&gt;ballardw&lt;/A&gt;'s elegant solution would be to replace the IF condition&lt;/P&gt;
&lt;PRE&gt;whichc(t[i],of a(*))&amp;gt;0&lt;/PRE&gt;
&lt;P&gt;by&lt;/P&gt;
&lt;PRE&gt;t[i] in a&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jan 2024 17:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913018#M359880</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-01-25T17:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify/shorten this Code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913072#M359896</link>
      <description>&lt;P&gt;Below a coding approach that will select rows where at least one variable contains a string Y1 ... Y100.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input (a1-a5 c f) ($);
datalines;
y1 . . . . x z
b b b b b
b a c d y3
d d d y2 y5
d d d d d y100 
d d d d d y101
;

data want;
  set have;
  array vars {*} a1-a5 c f;
  if prxmatch('/\b(y\d{1,2}|y100)\b/oi',strip(catx(' ',of vars[*])));
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2024 00:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/913072#M359896</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-26T00:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify/shorten this Code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/917722#M361511</link>
      <description>&lt;P&gt;That was very useful;&lt;/P&gt;&lt;P&gt;thanks to everyone, especially to you ballardw for your detailed explanations!&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2024 13:21:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-shorten-this-Code/m-p/917722#M361511</guid>
      <dc:creator>Curt</dc:creator>
      <dc:date>2024-02-24T13:21:28Z</dc:date>
    </item>
  </channel>
</rss>

