<?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: Macro for updating column based on occurrence of a substring in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691309#M19621</link>
    <description>&lt;P&gt;Thank you for your reply. The PUT can be disregarded as the column SUBID is now originally a character variable. Therefore, the updated code would be as shown below. The position within the search/resulting string will always be the same and the length of the string to be replaced will not change. The code does in fact do what I want. However, I was also interested in being able to have a&amp;nbsp;&lt;SPAN&gt;macro that I can call by inputting the original string and the output that I would like, or vice versa. For example the macro call&amp;nbsp;%mnemonic (10151024, 1445); would cause the updating of the string "H7891133 1029 10151024" and have the string "H7891133 1029&amp;nbsp;14451024" take its place in the data set, or something of that form.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data update_sub;
set subject_data;
select (substr(SUBID,15,8));
  when ('10151024') substr(SUBID,15,8) = "14451024";
  when ('10241005') substr(SUBID,15,8) = "15101005";
  when ('10241019') substr(SUBID,15,8) = "15101019";
  otherwise;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Oct 2020 15:23:15 GMT</pubDate>
    <dc:creator>AshJuri</dc:creator>
    <dc:date>2020-10-13T15:23:15Z</dc:date>
    <item>
      <title>Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/690984#M19612</link>
      <description>&lt;P&gt;Hi all, my code currently uses the tranwrd function in order to update a column based on the occurrence of a specified substring.&amp;nbsp; I would like to have a macro that achieves this in order to automate this process since my data set is quite large. Any solutions would be appreciated. My code is below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subject_data;
input SUBID;
datalines;                                         /*OUTPUT*/

H7891133 1029 10151024               /*H7891133 1029 14451024*/
H7891133 1029 10241005               /*H7891133 1029 15101005*/
H7891133 1029 10241019               /*H7891133 1029 15101019*/
;
run;

Data update_sub ;
		Set subject_data;
		SUBID_char = put(SUBID, 22.) ;
		/*drop SUBID ;
		rename SUBID_char=SUBID ; */

		if substr(SUBID_char,15,8)='10151024' then do 
		SUBID_char=tranwrd(SUBID_char, "10151024", "14451024");
   		put SUBID_char;
		end;

		if substr(SUBID_char,15,8)='10241005' then do 
		SUBID_char=tranwrd(SUBID_char, "10241005", "15101005");
		put SUBID_char;
		end;

		if substr(SUBID_char,15,8)='10241019' then do 
		SUBID_char=tranwrd(SUBID_char, "10241019", "15101019");
		put SUBID_char;
		end;
		
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 21:13:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/690984#M19612</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-12T21:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691000#M19614</link>
      <description>&lt;P&gt;Your code doesn't execute correctly, and I can't figure out what it's supposed to be doing. Get it running correctly, and provide a description of what's its supposed to be doing, and I'm sure it'll be easy to fix!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 15:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691000#M19614</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2020-10-12T15:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691001#M19615</link>
      <description>&lt;P&gt;Start by replacing your multiple IFs with a SELECT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (substr(SUBID_char,15,8));
  when ('10151024') substr(SUBID_char,15,8) = "14451024";
  when ('10241005') substr(SUBID_char,15,8) = "15101005";
  when ('10241019') substr(SUBID_char,15,8) = "15101019";
  otherwise;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code can then be automated by reading the "from" and "to" values from a dataset and creating the code by writing it to a temporary file and then including that at the proper location.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename inc _temp_;

data _null_;
set values;
file inc;
line = 'when ("' !! strip(from) !! '") substr(subid_char,15,8) = "' !! strip(to) !! '";';
put line;
run;

data want;
set have;
select (substr(subid_char,15,8));
  %include inc;
  otherwise;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 15:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691001#M19615</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-12T15:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691054#M19617</link>
      <description>&lt;P&gt;Hi, thank you very much for your reply. May I ask if you can provide an example of your solution filled with the corresponding inputs in order to call and have it execute? It would help greatly in understanding as I am fairly new to using macros. Thank you once again!&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 19:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691054#M19617</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-12T19:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691063#M19618</link>
      <description>&lt;P&gt;The dataset values would look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data values;
input (from to) (:$6.);
datalines;
10151024 14451024
10241005 15101005
10241019 15101019
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 19:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691063#M19618</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-12T19:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691089#M19619</link>
      <description>&lt;P&gt;Hi, thank you for your reply. I have updated my code and I believe it should execute. The input values are a snippet of values from a much larger data set. I replaced specified substrings in each value using the tranwrd function. For example, in string "H7891133 1029 10151024", the substring "10121024" is replaced by "14451024". Therefore, the output of the string is&amp;nbsp;H7891133 1029&amp;nbsp;14451024. I would like a macro that achieves this instead of my current approach so that I can just call the macro by inputting the original string and the output that I would like. For example %mnemonic("H7891133 1029 10151024","1445") should replace the string "H7891133 1029 10151024" and have the string "H7891133 1029&amp;nbsp;14451024" take its place in the data set, or something of that form.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 21:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691089#M19619</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-12T21:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691188#M19620</link>
      <description>&lt;P&gt;Let's start by clearly defining the code that is needed to do the actual work.&lt;/P&gt;
&lt;P&gt;Is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data update_sub;
set subject_data;
SUBID_char = put(SUBID, 22.);
select (substr(SUBID_char,15,8));
  when ('10151024') substr(SUBID_char,15,8) = "14451024";
  when ('10241005') substr(SUBID_char,15,8) = "15101005";
  when ('10241019') substr(SUBID_char,15,8) = "15101019";
  otherwise;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;doing what you want?&lt;/P&gt;
&lt;P&gt;If yes, which of the elements need to be changed to make the code dynamic?&lt;/P&gt;
&lt;P&gt;In particular, will the PUT always work as intended? Think of leading zeroes that would be omitted.&lt;/P&gt;
&lt;P&gt;Will the position within the search/resulting string always be the same?&lt;/P&gt;
&lt;P&gt;Will the length of the string to be replaced change?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 10:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691188#M19620</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-13T10:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691309#M19621</link>
      <description>&lt;P&gt;Thank you for your reply. The PUT can be disregarded as the column SUBID is now originally a character variable. Therefore, the updated code would be as shown below. The position within the search/resulting string will always be the same and the length of the string to be replaced will not change. The code does in fact do what I want. However, I was also interested in being able to have a&amp;nbsp;&lt;SPAN&gt;macro that I can call by inputting the original string and the output that I would like, or vice versa. For example the macro call&amp;nbsp;%mnemonic (10151024, 1445); would cause the updating of the string "H7891133 1029 10151024" and have the string "H7891133 1029&amp;nbsp;14451024" take its place in the data set, or something of that form.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data update_sub;
set subject_data;
select (substr(SUBID,15,8));
  when ('10151024') substr(SUBID,15,8) = "14451024";
  when ('10241005') substr(SUBID,15,8) = "15101005";
  when ('10241019') substr(SUBID,15,8) = "15101019";
  otherwise;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Oct 2020 15:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691309#M19621</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-13T15:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for updating column based on occurrence of a substring</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691330#M19622</link>
      <description>&lt;P&gt;As you can see, the only thing that is repeated now (with different values), is the WHEN statement within the SELECT.&lt;/P&gt;
&lt;P&gt;A macro would then incorporate this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mnemonic(in,out);
when ("&amp;amp;in") substr(subid,15,8) = "&amp;amp;out";
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and would be used like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data update_sub;
set subject_data;
select (substr(SUBID,15,8));
  %mnemonic(10151024,14451024)
  %mnemonic(10241005,15101005)
  %mnemonic(10241019,15101019)
  otherwise;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you see, that does not save much coding, because the SELECT block in itself is already a very efficient tool.&lt;/P&gt;
&lt;P&gt;If you have lots of these replacements to do, you are much better off automating the non-macro code with CALL EXECUTE or a dynamically built include.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 16:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Macro-for-updating-column-based-on-occurrence-of-a-substring/m-p/691330#M19622</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-13T16:17:15Z</dc:date>
    </item>
  </channel>
</rss>

