<?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: Modify part of the content of a string variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672579#M202146</link>
    <description>&lt;P&gt;The TRANWRD function can be used to replace occurrences of a given word in a string. For your scenario, for qualifying regions, you can replace 'rural' with 'urban'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this, for an example. Compare the before (have) and after (want) datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input region $ rate;
    datalines;
AN_rural 1
AD_rural 2
AR_rural 3
AS_rural 4 
BR_rural 5
CH_rural 6
CT_rural 7
DN_rural 8
DD_rural 9
DL_rural 10
GA_rural 11
GJ_rural 12
HR_rural 13
HP_rural 14
JK_rural 15
JH_rural 16
    ;
run;

data want;
    set have;
    if substr(region, 4, 5) = 'rural' then do;
        if rate &amp;lt; 10  /*you can modify this to your random number condition*/
            then region = tranwrd(region, 'rural', 'urban');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Jul 2020 15:28:12 GMT</pubDate>
    <dc:creator>mklangley</dc:creator>
    <dc:date>2020-07-27T15:28:12Z</dc:date>
    <item>
      <title>Modify part of the content of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672527#M202130</link>
      <description>&lt;P&gt;I have a string variable region. The first two characters are the region name followed by either _rural or _urban, such as:&lt;/P&gt;
&lt;P&gt;AN_rural&lt;/P&gt;
&lt;P&gt;AN_urban&lt;/P&gt;
&lt;P&gt;AR_rural&lt;/P&gt;
&lt;P&gt;AR_urban&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For observations in a rural region (all those ending by _rural), when a condition is meet (in short, with a random number is lower than a rate), I want to replace _rural by _urban. It should look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if substr(region,4,5)='rural' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;if rate&amp;lt;rand('uniform') then (replace _rural by_urban);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jul 2020 13:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672527#M202130</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2020-07-27T13:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Modify part of the content of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672541#M202133</link>
      <description>&lt;P&gt;from reading your request, it seems as though you have a column for region and then a column for observations.&amp;nbsp; based on what it sounds like you are trying to do, i would split the observations by region (you will end up with the observations as values under the columns for urban vs rural).&amp;nbsp; then you could use the case statement you are proposing ... note that you may want research the case statement as you likely will not need a replace function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i think something like this might work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if &lt;SPAN&gt;AN_rural&amp;nbsp;&amp;lt;rand('uniform')&amp;nbsp;&lt;/SPAN&gt;then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;AN_urban;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;AN_rural;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;without knowing more, the above is a guess and will surly need some work.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jul 2020 14:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672541#M202133</guid>
      <dc:creator>utrocketeng</dc:creator>
      <dc:date>2020-07-27T14:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Modify part of the content of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672546#M202135</link>
      <description>&lt;P&gt;That would work of course, but I have 35 regions (see below), so I would need to repeat this 35 times. Ideally, I would like to have a code that would just replace the "rural" by "urban"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AN_urban&lt;BR /&gt;AD_urban&lt;BR /&gt;AR_urban&lt;BR /&gt;AS_urban&lt;BR /&gt;BR_urban&lt;BR /&gt;CH_urban&lt;BR /&gt;CT_urban&lt;BR /&gt;DN_urban&lt;BR /&gt;DD_urban&lt;BR /&gt;DL_urban&lt;BR /&gt;GA_urban&lt;BR /&gt;GJ_urban&lt;BR /&gt;HR_urban&lt;BR /&gt;HP_urban&lt;BR /&gt;JK_urban&lt;BR /&gt;JH_urban&lt;BR /&gt;KA_urban&lt;BR /&gt;KL_urban&lt;BR /&gt;LD_urban&lt;BR /&gt;MP_urban&lt;BR /&gt;MH_urban&lt;BR /&gt;MN_urban&lt;BR /&gt;ML_urban&lt;BR /&gt;MZ_urban&lt;BR /&gt;NL_urban&lt;BR /&gt;OR_urban&lt;BR /&gt;PY_urban&lt;BR /&gt;PB_urban&lt;BR /&gt;RJ_urban&lt;BR /&gt;SK_urban&lt;BR /&gt;TN_urban&lt;BR /&gt;TR_urban&lt;BR /&gt;UT_urban&lt;BR /&gt;UP_urban&lt;BR /&gt;WB_urban&lt;BR /&gt;AN_rural&lt;BR /&gt;AD_rural&lt;BR /&gt;AR_rural&lt;BR /&gt;AS_rural&lt;BR /&gt;BR_rural&lt;BR /&gt;CH_rural&lt;BR /&gt;CT_rural&lt;BR /&gt;DN_rural&lt;BR /&gt;DD_rural&lt;BR /&gt;DL_rural&lt;BR /&gt;GA_rural&lt;BR /&gt;GJ_rural&lt;BR /&gt;HR_rural&lt;BR /&gt;HP_rural&lt;BR /&gt;JK_rural&lt;BR /&gt;JH_rural&lt;BR /&gt;KA_rural&lt;BR /&gt;KL_rural&lt;BR /&gt;LD_rural&lt;BR /&gt;MP_rural&lt;BR /&gt;MH_rural&lt;BR /&gt;MN_rural&lt;BR /&gt;ML_rural&lt;BR /&gt;MZ_rural&lt;BR /&gt;NL_rural&lt;BR /&gt;OR_rural&lt;BR /&gt;PY_rural&lt;BR /&gt;PB_rural&lt;BR /&gt;RJ_rural&lt;BR /&gt;SK_rural&lt;BR /&gt;TN_rural&lt;BR /&gt;TR_rural&lt;BR /&gt;UT_rural&lt;BR /&gt;UP_rural&lt;BR /&gt;WB_rural&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jul 2020 14:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672546#M202135</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2020-07-27T14:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Modify part of the content of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672579#M202146</link>
      <description>&lt;P&gt;The TRANWRD function can be used to replace occurrences of a given word in a string. For your scenario, for qualifying regions, you can replace 'rural' with 'urban'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this, for an example. Compare the before (have) and after (want) datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input region $ rate;
    datalines;
AN_rural 1
AD_rural 2
AR_rural 3
AS_rural 4 
BR_rural 5
CH_rural 6
CT_rural 7
DN_rural 8
DD_rural 9
DL_rural 10
GA_rural 11
GJ_rural 12
HR_rural 13
HP_rural 14
JK_rural 15
JH_rural 16
    ;
run;

data want;
    set have;
    if substr(region, 4, 5) = 'rural' then do;
        if rate &amp;lt; 10  /*you can modify this to your random number condition*/
            then region = tranwrd(region, 'rural', 'urban');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jul 2020 15:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672579#M202146</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-07-27T15:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Modify part of the content of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672674#M202182</link>
      <description>&lt;P&gt;Here's some sample code that uses the &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0n08xougp40i5n1xw7njpcy0a2b.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;SUBSTR right o&lt;/A&gt;f and&amp;nbsp;&lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0uev77ebdwy90n1rsd7hwjd2qc3.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;SUBSTR left of&lt;/A&gt; function&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data got ;
	input var $ ;
	do i=1 to 10 ;
		output ;
	end ;
cards ;
AN_rural
AN_urban
AR_rural
AR_urban
;
run ;

data want ;
	set got ;
	/* Check for "rural" */
	if substr(var,4,5)="rural" then
	do ;
		/* If random number &amp;lt; 0.5 */
		if ranuni(0)&amp;lt;0.5 then
		do ;
			put "Before : " var ;
			/* change rural to urban */
			substr(var,4,5)="urban" ;
			put "After : " var ;
		end ;
	end ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jul 2020 19:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-part-of-the-content-of-a-string-variable/m-p/672674#M202182</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2020-07-27T19:40:13Z</dc:date>
    </item>
  </channel>
</rss>

