<?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: Count number of times a number &amp;gt;= 1 appears in a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692589#M211004</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/333825"&gt;@CherylA&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Var = "0000000134564388889..."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create 72 variables that are 1 or 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e score_1 will be 1 if the first number in the Var string is &amp;gt;=1 and 0 if not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;score_2 will be 1 if the second number in the var string is &amp;gt;=1 and 0 if not,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll then sum the variables for each month using something like this to get a sum of the observations that were &amp;gt;=1 at each month:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data = data (keep=score_1-score_24) nway missing ;&lt;BR /&gt;var score_1-score_22;&lt;BR /&gt;output out = data_out (drop= _type_) sum= ;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is one way.&lt;/P&gt;
&lt;PRE&gt;data junk;
   Var = "0000000134564388889";
   array score_(24);
   do i=1 to length(var);
      score_[i] = (input(substr(var,i,1),best.)&amp;gt;1);
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;SAS comparisons will return 1 for true and 0 for false so after pulling the value, converting to numeric with input and substr, the comparison yields what was requested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But again why 72 variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may find it preferable to reshape the data to have a MONTH Value instead of hiding that information in a variable name. Which would be relatively easy for the example data.&lt;/P&gt;
&lt;PRE&gt;data junk2;
   Var = "0000000134564388889";
   do month=1 to length(var);
      score_ = (input(substr(var,month,1),best.)&amp;gt;1);
      output;
   end;
   drop var;
run;&lt;/PRE&gt;
&lt;P&gt;Your summary would use MONTH as a class or by variable, which would then have the NWAY actually do something.&lt;/P&gt;</description>
    <pubDate>Mon, 19 Oct 2020 15:05:52 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-10-19T15:05:52Z</dc:date>
    <item>
      <title>Count number of times a number &gt;= 1 appears in a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692472#M210978</link>
      <description>&lt;P&gt;Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Var = "0000000134564388889..."&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create 72 variables that are 1 or 0.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e score_1 will be 1 if the first number in the Var string is &amp;gt;=1 and 0 if not.&amp;nbsp;&lt;/P&gt;&lt;P&gt;score_2 will be 1 if the second number in the var string is &amp;gt;=1 and 0 if not,&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll then sum the variables for each month using something like this to get a sum of the observations that were &amp;gt;=1 at each month:&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc summary data = data (keep=score_1-score_24) nway missing ;&lt;BR /&gt;var score_1-score_22;&lt;BR /&gt;output out = data_out (drop= _type_) sum= ;&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 10:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692472#M210978</guid>
      <dc:creator>CherylA</dc:creator>
      <dc:date>2020-10-19T10:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of times a number &gt;= 1 appears in a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692506#M210991</link>
      <description>&lt;P&gt;You will need to explain a little more.&amp;nbsp; If you only have 24 months of data, why do you need 72 variables?&amp;nbsp; What would your first 7 new variables look like if you were working with a string that began with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Var = "&amp;gt;=1&amp;gt;=14 .....";&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 13:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692506#M210991</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-10-19T13:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of times a number &gt;= 1 appears in a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692589#M211004</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/333825"&gt;@CherylA&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Var = "0000000134564388889..."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create 72 variables that are 1 or 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e score_1 will be 1 if the first number in the Var string is &amp;gt;=1 and 0 if not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;score_2 will be 1 if the second number in the var string is &amp;gt;=1 and 0 if not,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll then sum the variables for each month using something like this to get a sum of the observations that were &amp;gt;=1 at each month:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data = data (keep=score_1-score_24) nway missing ;&lt;BR /&gt;var score_1-score_22;&lt;BR /&gt;output out = data_out (drop= _type_) sum= ;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is one way.&lt;/P&gt;
&lt;PRE&gt;data junk;
   Var = "0000000134564388889";
   array score_(24);
   do i=1 to length(var);
      score_[i] = (input(substr(var,i,1),best.)&amp;gt;1);
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;SAS comparisons will return 1 for true and 0 for false so after pulling the value, converting to numeric with input and substr, the comparison yields what was requested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But again why 72 variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may find it preferable to reshape the data to have a MONTH Value instead of hiding that information in a variable name. Which would be relatively easy for the example data.&lt;/P&gt;
&lt;PRE&gt;data junk2;
   Var = "0000000134564388889";
   do month=1 to length(var);
      score_ = (input(substr(var,month,1),best.)&amp;gt;1);
      output;
   end;
   drop var;
run;&lt;/PRE&gt;
&lt;P&gt;Your summary would use MONTH as a class or by variable, which would then have the NWAY actually do something.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 15:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-times-a-number-gt-1-appears-in-a-string/m-p/692589#M211004</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-19T15:05:52Z</dc:date>
    </item>
  </channel>
</rss>

