<?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 Automate my Data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603596#M174883</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have inherited some code (sample below) which is very manual whereby I am having to type in the text within the double quotation marks each time we get an updated file&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; work.all_order_1;&lt;/P&gt;
&lt;P&gt;set work.all_order_2;&lt;/P&gt;
&lt;P&gt;format order_name_grouped $300.;&lt;/P&gt;
&lt;P&gt;order_name_grouped=order_name;&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Top 100", 'i') ge 1 then order_name_grouped="Top 100";&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Cancelled", 'i') ge 1 then order_name_grouped="Cancelled";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a better way to write this out please so it automatically looks at the criteria and groups up please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 12 Nov 2019 15:03:54 GMT</pubDate>
    <dc:creator>KC_16</dc:creator>
    <dc:date>2019-11-12T15:03:54Z</dc:date>
    <item>
      <title>Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603596#M174883</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have inherited some code (sample below) which is very manual whereby I am having to type in the text within the double quotation marks each time we get an updated file&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; work.all_order_1;&lt;/P&gt;
&lt;P&gt;set work.all_order_2;&lt;/P&gt;
&lt;P&gt;format order_name_grouped $300.;&lt;/P&gt;
&lt;P&gt;order_name_grouped=order_name;&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Top 100", 'i') ge 1 then order_name_grouped="Top 100";&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Cancelled", 'i') ge 1 then order_name_grouped="Cancelled";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a better way to write this out please so it automatically looks at the criteria and groups up please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:03:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603596#M174883</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2019-11-12T15:03:54Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603599#M174885</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195960"&gt;@KC_16&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have inherited some code (sample below) which is very manual whereby I am having to type in the text within the double quotation marks each time we get an updated file&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; work.all_order_1;&lt;/P&gt;
&lt;P&gt;set work.all_order_2;&lt;/P&gt;
&lt;P&gt;format order_name_grouped $300.;&lt;/P&gt;
&lt;P&gt;order_name_grouped=order_name;&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Top 100", 'i') ge 1 then order_name_grouped="Top 100";&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Cancelled", 'i') ge 1 then order_name_grouped="Cancelled";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a better way to write this out please so it automatically looks at the criteria and groups up please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean (because you didn't say this) that there are a lot more than just the two lines that begin with IF FIND(... ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the real code has hundreds of these lines and they change from run to run?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603599#M174885</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-12T15:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603600#M174886</link>
      <description>&lt;P&gt;How many criteria do you have?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603600#M174886</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-12T15:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603602#M174888</link>
      <description>&lt;P&gt;Sorry, there are in total over 100 lines that follow this logic and each month, this continues to grow. However, each month I probably need to update 20 new rows to the existing list so it's not hugely time consuming but would be great to automate if possible&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603602#M174888</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2019-11-12T15:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603604#M174889</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195960"&gt;@KC_16&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Sorry, there are in total over 100 lines that follow this logic and each month, this continues to grow. However, each month I probably need to update 20 new rows to the existing list so it's not hugely time consuming but would be great to automate if possible&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Well, I think it is possible. However, you haven't mentioned how a programmer who will be automating this could know what the new rows will be? Where is this information stored?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603604#M174889</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-12T15:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603607#M174891</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195960"&gt;@KC_16&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have inherited some code (sample below) which is very manual whereby I am having to type in the text within the double quotation marks each time we get an updated file&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; work.all_order_1;&lt;/P&gt;
&lt;P&gt;set work.all_order_2;&lt;/P&gt;
&lt;P&gt;format order_name_grouped $300.;&lt;/P&gt;
&lt;P&gt;order_name_grouped=order_name;&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Top 100", 'i') ge 1 then order_name_grouped="Top 100";&lt;/P&gt;
&lt;P&gt;if &lt;EM&gt;find&lt;/EM&gt;(order_name,"Cancelled", 'i') ge 1 then order_name_grouped="Cancelled";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a better way to write this out please so it automatically looks at the criteria and groups up please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My personal take on this where you are having to search text to find changing values is that your process is suboptimal or even seriously flawed. I ask "Why do these important values change so frequently?" I say important because if you are searching for them there must be some importance attached.&lt;/P&gt;
&lt;P&gt;Perhaps this is an indication of poor data entry design, training or data specification.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF, and this a pretty big if given that you say you have a hundred lines similar to this, where the found value is used to&amp;nbsp;set to the value of the variable order_named_group there are likely many ways. However you do know that if the same response contains "Top 100" and "Cancelled" only the cancelled is set don't you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One method would be a temporary array:&lt;/P&gt;
&lt;PRE&gt;data example;
   input order_name $ 1-50;
   array t {2} $ 15 _temporary_ ('Top 100','Cancelled');
   length order_name_grouped $ 15;
   do i= 1 to dim(t);
      if find(order_name,strip(t[i]), 'i') ge 1 then order_name_grouped=t[i]; 
   end;
   drop i;
datalines;
Something Top 100 is in this text
Or cancelled is part of this line
;&lt;/PRE&gt;
&lt;P&gt;which moves all of the "typing" into one place. The number of items in the array, the lengths and the value typed have to match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am also a bit concerned about the 300 length for order_named_grouped as that seems to indicate your values have that length. Which is truly cumbersome for "typing" in the first place.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values are in a data set some where then other options open up.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603607#M174891</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-12T15:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Automate my Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603860#M174986</link>
      <description>&lt;P&gt;Just a dumb question? What if your ORDER_NAME variable contains both "Top 100" and "Cancelled"? Will this never happen? Or is there a priority? As your program is written, an ORDER_NAME containing both strings will get the value "Cancelled", as you have no ELSE statement after the first IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, if you have your values in SAS data set, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data search_texts:
  infile cards truncover;
  input priority text $30.;
cards;
1 Cancelled
2 Top 100
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tempsas temp;
data _null_;
  set search_texts;
  text=quote(trim(text),"'");
  file tempsas;
  put 'when(find(order_name,' text ", 'i') ge 1) order_name_grouped=" text ';';
run;

data want;
  set have;
&amp;nbsp;&amp;nbsp;length&amp;nbsp;Order_Name_grouped&amp;nbsp;$300;
&amp;nbsp;&amp;nbsp;select;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%include&amp;nbsp;tempsas;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;otherwise&amp;nbsp;Order_Name_Grouped=Order_Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that your data is sorted with the highest priority first (if there are any ORDER_NAME values with more than one search string in it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have your strings in a text file, you can try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro search(text);
when(find(order_name,"&amp;amp;text" , 'i') ge 1) order_name_grouped="&amp;amp;text" ;
%mend;

data want;
  length Order_Name_Grouped $300;
  select;
    %search(Top 100               );
    %search(Cancelled             );
    otherwise Order_Name_Grouped=Order_Name;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The idea is just to use the block mark capability in SAS.&lt;/P&gt;
&lt;P&gt;First repeat the %search lines so many times in your program that you have enough lines for all the strings&lt;/P&gt;
&lt;P&gt;Open your text file in a SAS editor, and mark the strings with the mouse &lt;STRONG&gt;while holding down the ALT key&amp;nbsp;&lt;/STRONG&gt;(that's the block mark).&lt;/P&gt;
&lt;P&gt;Copy that (Ctrl-C).&lt;/P&gt;
&lt;P&gt;Block mark the area between the parantheses in the macro calls your SAS program.&lt;/P&gt;
&lt;P&gt;Paste (Ctrl-V).&lt;/P&gt;
&lt;P&gt;Remove macro calls with empty parameters, if you made too many lines. If you made too few lines with macro calls, your program will look garbled in the end - in that case, press undo (Ctrl-Z), make some more macro call lines, and try again.&lt;/P&gt;
&lt;P&gt;Remove the extra lines you made in the start, if you made too many.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just remember that this way, again, the strings with highest priority should be first.&lt;/P&gt;
&lt;P&gt;Of course, you could also read your text file with a SAS data step, and then apply the first solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 13:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automate-my-Data-step/m-p/603860#M174986</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-13T13:58:38Z</dc:date>
    </item>
  </channel>
</rss>

