<?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: Counting character variable in array with substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373463#M89347</link>
    <description>&lt;P&gt;Sound like you really want something like the following:&lt;/P&gt;
&lt;PRE&gt;proc import datafile="/folders/myfolders/patient_nodups.csv" 
     out=have
     dbms=csv replace;
  getnames=yes;
run;

proc sql noprint;
  select catx(' ','length',name, '$6;')
    into :lengths separated by ' '
      from dictionary.columns
        where libname=upcase('work') and
              memname=upcase('have') and
              substr(name,1,5) eq 'Other'
  ;
quit;

data qcodes (keep=facility_name qcode);
  &amp;amp;lengths.;
  set have;
  array qcodes{*} $ Other_Diagnosis_Code_24-Other_Diagnosis_Code_1;
  do i = 1 to dim(qcodes);
    if first(qcodes(i)) eq 'Q' then do;
      qcode=substr(qcodes(i),1,3);
      output;
    end;
  end;
run;

proc sort data=qcodes;
  by facility_name;
run;

proc freq data=qcodes;
  by facility_name;
  tables qcode/out=want (drop=percent);
run;

proc transpose data=want out=want (drop=_:);
  by facility_name;
  var count;
  id qcode;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jul 2017 00:29:06 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-07-06T00:29:06Z</dc:date>
    <item>
      <title>Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373359#M89315</link>
      <description>&lt;P&gt;I'd like to count first three digits of Qcodes by Facility name. I don't need other codes&amp;nbsp;for now. Below shows my attempt to&amp;nbsp;substract first three digits of Q codes then count by facility name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="....\patient_nodups.csv" 
out=have
dbms=csv replace;
getnames=yes;
run;

data qcodes; set Patients_NODUPS; &lt;BR /&gt; array qcodes {*} Other_Diagnosis_Code_1-Other_Diagnosis_Code_24;&lt;BR /&gt; do i = 1 to dim(qcodes);&lt;BR /&gt; if substr(left(qcodes),1,3){i} in ('Q:') then output qcodes;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for being such a guradian angel.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 19:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373359#M89315</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-07-05T19:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373368#M89319</link>
      <description>&lt;P&gt;Not clear what you are trying to achieve. If you want to eliminate any of the other_diagnostic_codes that don't start with "Q", then the following will work:&lt;/P&gt;
&lt;PRE&gt;proc import datafile="/folders/myfolders/patient_nodups.csv" 
     out=have
     dbms=csv replace;
  getnames=yes;
run;

proc sql noprint;
  select catx(' ','length',name, '$6;')
    into :lengths separated by ' '
      from dictionary.columns
        where libname=upcase('work') and
              memname=upcase('have') and
              substr(name,1,5) eq 'Other'
  ;
quit;

data qcodes;
  &amp;amp;lengths.;
  set have;
  array qcodes{*} $ Other_Diagnosis_Code_24-Other_Diagnosis_Code_1;
  do i = 1 to dim(qcodes);
    if first(qcodes(i)) ne 'Q' then call missing(qcodes(i));
  end;
  call sortc(of qcodes(*));
run;
&lt;/PRE&gt;
&lt;P&gt;The reason the proc sql code is there is because proc import set different lengths for each of the other_diagnostic_code variables and the proc sql code standardizes them all to have a length of 6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 17:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373368#M89319</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-05T17:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373377#M89324</link>
      <description>&lt;P&gt;I just tried below code wondering if I'm misplacinf end;. But no success yet.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data qcodes; set have; 
        array qcodes {*} Other_Diagnosis_Code_1-Other_Diagnosis_Code_24;
            do i = 1 to dim(qcodes);
			end;
            if (substr(left(qcodes),1,3)) in ('Q:') then output qcodes;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Log:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      1060:29
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.QCODES may be incomplete.  When this step was stopped there were 0
         observations and 36 variables.
WARNING: Data set WORK.QCODES was not replaced because this step was stopped.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2017 17:46:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373377#M89324</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-07-05T17:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373379#M89325</link>
      <description>&lt;P&gt;If I've figured out what you are after, here's a way to get all the Q codes for each facility.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data qcodes;&lt;/P&gt;
&lt;P&gt;array codes {24} $ 4 Other_diagnosis_code_1 - Other_diagnosis_code_24;&lt;/P&gt;
&lt;P&gt;set have (keep=facility other_diagnosis_code_: );&lt;/P&gt;
&lt;P&gt;do i=1 to 24;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if codes{i} =: 'Q' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; qcode = codes{i};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep facility qcode;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;tables facility * qcode / missing list;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Defining the array with length of $4 for each element ahead of time means you get Q plus the three digits that follow.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 21:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373379#M89325</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-05T21:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373402#M89333</link>
      <description>&lt;P&gt;If I understand correctly, try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data qcodes;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set Patients_NODUPS;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;array qcodes {*} Other_Diagnosis_Code_1-Other_Diagnosis_Code_24;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do i = 1 to dim(qcodes);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if substr(left(qcodes),1,1){i}='Q' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 18:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373402#M89333</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2017-07-05T18:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373444#M89344</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you very much.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options nolabel;
proc sql noprint;
  select catx(' ','length',name, '$6;')
    into :lengths separated by ' '
      from dictionary.columns
        where libname=upcase('work') and
              memname=upcase('patients_nodups') and
              substr(name,1,3) eq 'Other'
  ;
quit;

data qcodes;
  &amp;amp;lengths.;
  set patients_nodups;
  array qcodes{*} $ Other_Diagnosis_Code_24-Other_Diagnosis_Code_1;
  do i = 1 to dim(qcodes);
    if first(qcodes(i)) ne 'Q' then call missing(qcodes(i));
  end;
  call sortc(of qcodes(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;worked fine with warning:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WARNING: Multiple lengths were specified for the variable Other_Diagnosis_Code_1 by input data
         set(s). This can cause truncation of data.
thru &lt;BR /&gt;
WARNING: Multiple lengths were specified for the variable Other_Diagnosis_Code_24 by input data
         set(s). This can cause truncation of data.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I changed&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;substr(name,1,5) to substr(name,1,3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;in order to substract first three digits of q codes. However, I have 5 digit Q codes in the output data. The reason why is that I'd like to count number of distinct 3 digit qcodes by facility name for the next step. I apologize for ambiguity in my question. Hope image helps.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am I missing something here?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10140i6D9CBB0365197C11/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="N of distinct q codes by hospitals.png" title="N of distinct q codes by hospitals.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 21:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373444#M89344</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-07-05T21:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting character variable in array with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373463#M89347</link>
      <description>&lt;P&gt;Sound like you really want something like the following:&lt;/P&gt;
&lt;PRE&gt;proc import datafile="/folders/myfolders/patient_nodups.csv" 
     out=have
     dbms=csv replace;
  getnames=yes;
run;

proc sql noprint;
  select catx(' ','length',name, '$6;')
    into :lengths separated by ' '
      from dictionary.columns
        where libname=upcase('work') and
              memname=upcase('have') and
              substr(name,1,5) eq 'Other'
  ;
quit;

data qcodes (keep=facility_name qcode);
  &amp;amp;lengths.;
  set have;
  array qcodes{*} $ Other_Diagnosis_Code_24-Other_Diagnosis_Code_1;
  do i = 1 to dim(qcodes);
    if first(qcodes(i)) eq 'Q' then do;
      qcode=substr(qcodes(i),1,3);
      output;
    end;
  end;
run;

proc sort data=qcodes;
  by facility_name;
run;

proc freq data=qcodes;
  by facility_name;
  tables qcode/out=want (drop=percent);
run;

proc transpose data=want out=want (drop=_:);
  by facility_name;
  var count;
  id qcode;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2017 00:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-character-variable-in-array-with-substr/m-p/373463#M89347</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-06T00:29:06Z</dc:date>
    </item>
  </channel>
</rss>

