<?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 do loops and if then statements for numeric / character variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214686#M39622</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm new to SAS , and I'm really tired of this problem I keep running into, but I can't figure it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to categorize data all the time with new character variables based on numeric values of another variable, e.g.:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works fine:&lt;/P&gt;&lt;P&gt;if dm_prev_adj &amp;lt; Q1_dm then dm_prev_lvl = 'Low';&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The "issue" is that it is horribly inefficient.&amp;nbsp; I have to do this for each level, for each variable.. in this instance it is only nine lines of code (3 variables, 3 levels), but I have to believe that a do loop can do this easier. When I use a do loop and a numeric value it works fine for numbers:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;do i = 1 to 3;&lt;/P&gt;&lt;P&gt;if adj(i) &amp;lt;= q1(i) then lvl(i)=1;&lt;/P&gt;&lt;P&gt;else if q1(i) &amp;lt; adj(i) &amp;lt; q3(i) then lvl(i)=2;&lt;/P&gt;&lt;P&gt;else lvl(i)=3;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if I change those numeric values to words, aka 'Low' 'Medium' 'High' it bombs out and says:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'Low' , at line 1451 column 32.&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'High' , at line 1453 column 13.&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'Medium' , at line 1452 column 44&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;over and over and over...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems really easy but I cannot find the answer, would appreciate any help or alternatives.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 31 Jul 2015 00:00:25 GMT</pubDate>
    <dc:creator>Tekkers</dc:creator>
    <dc:date>2015-07-31T00:00:25Z</dc:date>
    <item>
      <title>do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214686#M39622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm new to SAS , and I'm really tired of this problem I keep running into, but I can't figure it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to categorize data all the time with new character variables based on numeric values of another variable, e.g.:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works fine:&lt;/P&gt;&lt;P&gt;if dm_prev_adj &amp;lt; Q1_dm then dm_prev_lvl = 'Low';&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The "issue" is that it is horribly inefficient.&amp;nbsp; I have to do this for each level, for each variable.. in this instance it is only nine lines of code (3 variables, 3 levels), but I have to believe that a do loop can do this easier. When I use a do loop and a numeric value it works fine for numbers:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;do i = 1 to 3;&lt;/P&gt;&lt;P&gt;if adj(i) &amp;lt;= q1(i) then lvl(i)=1;&lt;/P&gt;&lt;P&gt;else if q1(i) &amp;lt; adj(i) &amp;lt; q3(i) then lvl(i)=2;&lt;/P&gt;&lt;P&gt;else lvl(i)=3;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if I change those numeric values to words, aka 'Low' 'Medium' 'High' it bombs out and says:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'Low' , at line 1451 column 32.&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'High' , at line 1453 column 13.&lt;/P&gt;&lt;P&gt;NOTE: Invalid numeric data, 'Medium' , at line 1452 column 44&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;over and over and over...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems really easy but I cannot find the answer, would appreciate any help or alternatives.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 00:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214686#M39622</guid>
      <dc:creator>Tekkers</dc:creator>
      <dc:date>2015-07-31T00:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214687#M39623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When you define your array, you need to define the elements as being character.&amp;nbsp; (They will be numeric by default.)&amp;nbsp; It's as easy as this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;array lvl {3} $ 6 lvl1-lvl3;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 00:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214687#M39623</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-07-31T00:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214688#M39624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with @Astounding, alternatively you can try &lt;STRONG&gt;do over&lt;/STRONG&gt; , its another way of&amp;nbsp; using arrays&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;array name $ m1-m3;&lt;/P&gt;&lt;P&gt;do over name;&lt;/P&gt;&lt;P&gt;name= .....conversion code........;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 01:00:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214688#M39624</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2015-07-31T01:00:38Z</dc:date>
    </item>
    <item>
      <title>Re: do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214689#M39625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi ... in addition to declaring a character array as "Astounding" has described, another way to ensure that you have a character array is to place the ARRAY statement in the data step after the SET statement.&amp;nbsp; That way, SAS already knows the variable type when you use the ARRAY statement ... assuming that the variable names match the array name (e.g. array lvl comprises lvl1 lvl2 lvl3)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data new;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;set old;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array lvl(3);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array adj(3);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array q1(3);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array q3(3);&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do i = 1 to 3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; if adj(i) &amp;lt;= q1(i) then lvl(i)=1;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; else if q1(i) &amp;lt; adj(i) &amp;lt; q3(i) then lvl(i)=2;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; else lvl(i)=3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 13px; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 01:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214689#M39625</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-07-31T01:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214690#M39626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Astounding and to all-- I really had hoped it was something simple, but had no idea it was that simple!&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 01:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214690#M39626</guid>
      <dc:creator>Tekkers</dc:creator>
      <dc:date>2015-07-31T01:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: do loops and if then statements for numeric / character variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214691#M39627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the code of mikezeb could be modified with do over as below , please try . hope it helps&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data new;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;set old;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array lvl lvl1-lvl3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array adj adj1 - adj3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array q1 _q1-_q3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-size: 13px; background-color: #ffffff;"&gt;array q3 _qs1-_qs3;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do over lvl;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; if adj(i) &amp;lt;= q1(i) then lvl(i)=1;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; else if q1(i) &amp;lt; adj(i) &amp;lt; q3(i) then lvl(i)=2;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; else lvl(i)=3;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; background-color: #ffffff; font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; background-color: #ffffff; font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; background-color: #ffffff; font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;Thanks,&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; background-color: #ffffff; font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;Jag&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Jul 2015 01:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-loops-and-if-then-statements-for-numeric-character-variables/m-p/214691#M39627</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2015-07-31T01:51:56Z</dc:date>
    </item>
  </channel>
</rss>

