<?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: Creating new variable from array in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86729#M24779</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your new_variable is being reset at every iteration of the loop, so it will only show a value of 1 if Reason_Code_10 begins with 'RR'.&lt;/P&gt;&lt;P&gt;The solution from &lt;A __default_attr="5068" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt; solves this problem, another option is to put an UNTIL statement in the do loop so that it stops processing when 'RR' is encountered&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 10 until (R_indicator{i}=:'RR');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yet another possibility, if the letters 'RR' only occur at the start of the string (or you want to search for them anywhere in the string), is to drop the array completely and use the INDEX and CATX functions together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_variable=index(catx(',',of Reason_Code:),'RR')&amp;gt;0;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 23 Jul 2013 13:19:32 GMT</pubDate>
    <dc:creator>Keith</dc:creator>
    <dc:date>2013-07-23T13:19:32Z</dc:date>
    <item>
      <title>Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86725#M24775</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi I would like to create a new variable based on the result of a array. so it will loop through 10 variables and if the variables match where the variable is like 'RR' then the new variable will show a 1 or a zero.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the code I have so far.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data year2012;&lt;BR /&gt;set exphandsql;&lt;BR /&gt;if total_turnover = . then total_turnover = 0;&lt;/P&gt;&lt;P&gt;array R_indicator(10) Reason_Code_1-Reason_Code_10;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i = 1 to 10 ; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if R_indicator&lt;I&gt; =:'RR' then &amp;lt;new variable&amp;gt;= '1' ;&lt;BR /&gt;else &amp;lt;new variable&amp;gt;='0';&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; drop i;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It doesn't seem to working properly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 12:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86725#M24775</guid>
      <dc:creator>aivoryuk</dc:creator>
      <dc:date>2013-07-23T12:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86726#M24776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Aivoryuk,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are potentially 2 errors. First, if Reason_Code_i variables are not of length 2, then R_indicator&lt;I&gt; would actually be "RR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; " aka white space padding up to the length of the variable. Use trim function to fix this&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if trim(R_indicator&lt;I&gt;)='RR' then newvar='1';&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second error is a logical one. Since you loop on all 10 codes and you have an else stement, the else statement will apply every step of the loop where the value isn't RR. Thus, unless trim(reason_code_10)='RR' you will always see 0.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To fix this, you can either remove it all together if you don't mind missing values instead of 0 or remove the else statement and add a if statement outside the loop. That is, something like&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if newvar=. then newvar=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 12:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86726#M24776</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-07-23T12:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86727#M24777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;when there is a RR in reason_code_10 in will work, else it will always be 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="text-decoration: underline;"&gt;test this:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data year2012;&lt;BR /&gt;set sashelp.class;&lt;/P&gt;&lt;P&gt;array&amp;nbsp; R_indicator(2) $ name sex;&lt;BR /&gt;array&amp;nbsp; Flag(2) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i = 1 to 2 ; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if R_indicator&lt;I&gt; =: 'M' then flag&lt;I&gt;=1 ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else flag&lt;I&gt;=.;&lt;BR /&gt;&amp;nbsp; end;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; test=nmiss(of flag(*)) ; &lt;/P&gt;&lt;P&gt;&amp;nbsp; drop i ; &lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;when nothing is found all flag are missing&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;JAHEUK&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 12:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86727#M24777</guid>
      <dc:creator>Jaheuk</dc:creator>
      <dc:date>2013-07-23T12:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86728#M24778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My primary concern is how you construct your "new variable". Is it one "new variable" for each different "Reason_Code", or just ONE "new variable" for all of them, and if either of "Reason_Code" starts with 'RR', the "new variable" will be 1, otherwise will be 0?&lt;/P&gt;&lt;P&gt;If it is the former, I think &lt;A __default_attr="527942" __jive_macro_name="user" class="jive_macro jive_macro_user" href="https://communities.sas.com/"&gt;&lt;/A&gt; has offered an answer; if it is the latter, try this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data year2012;&lt;BR /&gt;set exphandsql;&lt;BR /&gt;if total_turnover = . then total_turnover = 0;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;array R_indicator(10) Reason_Code_1-Reason_Code_10;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;new_variable='0';&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp; do i = 1 to 10 ; &lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_variable=ifn(R_indicator&lt;I&gt; =:'RR', '1', new_variable) ;&lt;/I&gt;&lt;/STRONG&gt;&lt;I&gt;&lt;/I&gt;&lt;/EM&gt;&lt;I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp; end;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; drop i;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Or more efficiently:&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;new_variable='0';&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp; do i = 1 to 10 ; &lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if R_indicator&lt;I&gt; =:'RR' then do; new_variable= '1'; leave;end;&lt;/I&gt;&lt;/STRONG&gt;&lt;I&gt;&lt;/I&gt;&lt;/EM&gt;&lt;I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp; end;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;SPAN style="text-decoration: underline;"&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 12:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86728#M24778</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-07-23T12:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86729#M24779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your new_variable is being reset at every iteration of the loop, so it will only show a value of 1 if Reason_Code_10 begins with 'RR'.&lt;/P&gt;&lt;P&gt;The solution from &lt;A __default_attr="5068" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt; solves this problem, another option is to put an UNTIL statement in the do loop so that it stops processing when 'RR' is encountered&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 10 until (R_indicator{i}=:'RR');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yet another possibility, if the letters 'RR' only occur at the start of the string (or you want to search for them anywhere in the string), is to drop the array completely and use the INDEX and CATX functions together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_variable=index(catx(',',of Reason_Code:),'RR')&amp;gt;0;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 13:19:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86729#M24779</guid>
      <dc:creator>Keith</dc:creator>
      <dc:date>2013-07-23T13:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86730#M24780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;@hai.kuo Am I going blind or did you add the last bit with the LEAVE statement in your original post, or post it in an edit?&amp;nbsp; I almost posted that exact solution, but decided to use UNTIL instead. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 13:24:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86730#M24780</guid>
      <dc:creator>Keith</dc:creator>
      <dc:date>2013-07-23T13:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86731#M24781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;LOL, Keith. I updated it within 1 minute, figured there should be no difference, guess I was wrong.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 13:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86731#M24781</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-07-23T13:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86732#M24782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A __default_attr="5068" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt; I'll cancel that call with the opticians then...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 13:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86732#M24782</guid>
      <dc:creator>Keith</dc:creator>
      <dc:date>2013-07-23T13:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86733#M24783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Inspired by &lt;A __default_attr="7766" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt; 's second suggestion, here is one approach that only counts the starting 'RR',&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;new_variable=prxmatch('/\bRR/',catx(' ',of Reason_Code:))&amp;gt;0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 14:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86733#M24783</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-07-23T14:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable from array</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86734#M24784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks guys that really helped as well helping my understanding&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jul 2013 14:35:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-variable-from-array/m-p/86734#M24784</guid>
      <dc:creator>aivoryuk</dc:creator>
      <dc:date>2013-07-23T14:35:39Z</dc:date>
    </item>
  </channel>
</rss>

