<?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 replacing occurrence of substring in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689717#M209687</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/317115"&gt;@AshJuri&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your reply. I have a column called "subjectID" in a data set. I would like to update that column by replacing the occurrence of a sequence of numbers with a specified number. For example, I would like "1357" to replace "1055" in "10551024" in order to become "13571024". I provided the example data in order to show the type of replacement I was trying to achieve. I wanted to use the macro to achieve this.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So how many of these do you have to replace?&lt;/P&gt;
&lt;P&gt;Another approach. I hope it is obvious where your old and new values are going.&lt;/P&gt;
&lt;PRE&gt;data testdata;
   infile datalines;
   input  sub $;
   select (substr(sub,1,4));
      when ('1055') substr(sub,1,4)='1357';     
      when ('1036') substr(sub,1,4)='1671';     
      when ('1089') substr(sub,1,4)='1752';     
      otherwise;
   end;
datalines;                        
10551024    
10361072    
10891012    
11111111
;&lt;/PRE&gt;
&lt;P&gt;I include a value of your Sub variable that doesn't have any replacement to show nothing happens.&lt;/P&gt;
&lt;P&gt;The Select/when/end is like having a bunch of If/then/else statements. The WHEN contains one or a comma separated list of values. The values should match something from the value after the SELECT, in this case the first four characters of the string.&lt;/P&gt;
&lt;P&gt;The Otherwise statement lets you do something for values that do not match any of the when lists.&lt;/P&gt;</description>
    <pubDate>Wed, 07 Oct 2020 20:15:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-10-07T20:15:54Z</dc:date>
    <item>
      <title>Macro for replacing occurrence of substring in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689660#M209673</link>
      <description>&lt;P&gt;Hello all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I created a macro for replacing the occurrence of a substring in a variable with a specified number. However, it is returning an error stating "Statement is not valid or it is used out of proper order." Any help would be appreciated.&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data testdata;
  infile datalines;
  input oldsub newsub sub;
datalines;                        OUTPUT           
1055	1357	10551024          13571024
1036	1671	10361072          16711072
1089	1752	10891012          17521012
;
run;

%macro mnemonic(update_subjid, new);
	if substr('&amp;amp;update_subjid',1,4)='1055' 
		then do; 
			&amp;amp;new || substr('update_subjid',5,8);
	end;
%mend mnemonic;

%mnemonic (10551024, 1357);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 18:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689660#M209673</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-07T18:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for replacing occurrence of substring in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689664#M209675</link>
      <description>&lt;P&gt;Your macro is supposed to create data step code, but you use it outside of a data step.&lt;/P&gt;
&lt;P&gt;Before you try to make code dynamic, make sure it works. Please show us your code for a single instance.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 18:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689664#M209675</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-07T18:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for replacing occurrence of substring in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689665#M209676</link>
      <description>&lt;P&gt;Your macro contains data step code that would have to be part of a data step.&lt;/P&gt;
&lt;P&gt;Your macro variable&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;substr('&amp;amp;update_subjid',1,4)&lt;/LI-CODE&gt;
&lt;P&gt;will not be used because macro variables have to be between double quotes.&lt;/P&gt;
&lt;P&gt;And you provided one single fixed value to compare, not the variable name, so the values of the variable are not tested.&lt;/P&gt;
&lt;P&gt;Your example data has all the values as numeric so you have some issues with the actual values involved as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the object is to actually use the values in the data set then:&lt;/P&gt;
&lt;PRE&gt;data testdata;
  infile datalines;
  input oldsub $ newsub $ sub $;
  if sub =: strip(oldsub) then substr(sub,1,4)=strip(newsub);
datalines;                        
1055	1357	10551024    
1036	1671	10361072    
1089	1752	10891012    
;&lt;/PRE&gt;
&lt;P&gt;Otherwise describe the issue in more detail. If you do not actually have oldsub and newsub in the data then show us what you actually have.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 18:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689665#M209676</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-07T18:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for replacing occurrence of substring in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689667#M209677</link>
      <description>&lt;P&gt;Thank you for your reply. I have a column called "subjectID" in a data set. I would like to update that column by replacing the occurrence of a sequence of numbers with a specified number. For example, I would like "1357" to replace "1055" in "10551024" in order to become "13571024". I provided the example data in order to show the type of replacement I was trying to achieve. I wanted to use the macro to achieve this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 18:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689667#M209677</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-07T18:55:48Z</dc:date>
    </item>
    <item>
      <title>Updating a column in a data set using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689668#M209689</link>
      <description>&lt;P&gt;HI,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a column called "subjid" in a dataset called "patients".&lt;SPAN&gt;&amp;nbsp; I would like to update that column by replacing the occurrence of a sequence of numbers with a specified number using a macro. For example, I would like "1357" to replace "1055" in "10551024" in order to become "13571024". Below is my code. However, I am receiving an error stating "Statement is not valid or it is used out of proper order." How can I fix this? Thank you in advance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mnemonic(update_subjid, new);
	if substr('&amp;amp;update_subjid',1,4)='1055' 
		then do; 
			&amp;amp;new || substr('update_subjid',5,8);
	end;
%mend mnemonic;

%mnemonic (10551024, 1357)&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 19:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689668#M209689</guid>
      <dc:creator>AshJuri</dc:creator>
      <dc:date>2020-10-07T19:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for replacing occurrence of substring in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689717#M209687</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/317115"&gt;@AshJuri&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your reply. I have a column called "subjectID" in a data set. I would like to update that column by replacing the occurrence of a sequence of numbers with a specified number. For example, I would like "1357" to replace "1055" in "10551024" in order to become "13571024". I provided the example data in order to show the type of replacement I was trying to achieve. I wanted to use the macro to achieve this.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So how many of these do you have to replace?&lt;/P&gt;
&lt;P&gt;Another approach. I hope it is obvious where your old and new values are going.&lt;/P&gt;
&lt;PRE&gt;data testdata;
   infile datalines;
   input  sub $;
   select (substr(sub,1,4));
      when ('1055') substr(sub,1,4)='1357';     
      when ('1036') substr(sub,1,4)='1671';     
      when ('1089') substr(sub,1,4)='1752';     
      otherwise;
   end;
datalines;                        
10551024    
10361072    
10891012    
11111111
;&lt;/PRE&gt;
&lt;P&gt;I include a value of your Sub variable that doesn't have any replacement to show nothing happens.&lt;/P&gt;
&lt;P&gt;The Select/when/end is like having a bunch of If/then/else statements. The WHEN contains one or a comma separated list of values. The values should match something from the value after the SELECT, in this case the first four characters of the string.&lt;/P&gt;
&lt;P&gt;The Otherwise statement lets you do something for values that do not match any of the when lists.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 20:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689717#M209687</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-07T20:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: Updating a column in a data set using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689924#M209790</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/317115"&gt;@AshJuri&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe the earlier request was this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show us working DATA step code, &lt;STRONG&gt;with no macros and no macro variables&lt;/STRONG&gt;, that does the task you want on a single input ID. That's where the solution starts.&amp;nbsp;If you can't get working DATA step code, with no macros and no macro variables, on a single input ID, then your macro will never work either.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 10:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-replacing-occurrence-of-substring-in-a-variable/m-p/689924#M209790</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-08T10:35:17Z</dc:date>
    </item>
  </channel>
</rss>

