<?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: All variables in array list must be the same type, i.e., all numeric or character. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811513#M320099</link>
    <description>&lt;P&gt;Show us a portion of the .txt file&lt;/P&gt;</description>
    <pubDate>Wed, 04 May 2022 16:56:13 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-05-04T16:56:13Z</dc:date>
    <item>
      <title>All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811473#M320075</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a huge data with lab results (25 fields) and it is dirty.&amp;nbsp; I will need to categorize the final (clean) results for my analysis but before that step I need to clean the unwanted characters such as "=","ug/dl", "&amp;lt;" or "&amp;gt;" comes along with the numerical result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to use array and do loops because I have 25 variables.&amp;nbsp; I want to set anything below 5 to "1".&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data wanted;&lt;BR /&gt;set have;&lt;/P&gt;&lt;P&gt;array AResult(25)&amp;nbsp; Result_1-Result_25;&lt;/P&gt;&lt;P&gt;do i=1 to 25;&lt;/P&gt;&lt;P&gt;if AResult(25) in ('&amp;lt;0.5','3.8ug/dl','4.0ug/dl','4.1ug/dl','4.2ug/dl','&amp;lt;.3.3','&amp;lt;5','LOW','LOW &amp;lt;3','=1.3')&lt;BR /&gt;then AResult{25}=1;&lt;BR /&gt;else AResult{25}=AResult{25};&lt;BR /&gt;END;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting this error:ERROR: All variables in array list must be the same type, i.e., all numeric or character.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I handle this error?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example Data:&lt;/P&gt;&lt;P&gt;data work.patients;&lt;BR /&gt;infile datalines dlm=",";&lt;BR /&gt;input pateint_id Result_1 Result_2 Result_3;&lt;BR /&gt;datalines;&lt;BR /&gt;1, =70, 65, 82&lt;BR /&gt;2, 88, =75, 79ug/dl&lt;BR /&gt;3, 64, 72,=80;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 14:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811473#M320075</guid>
      <dc:creator>sasuser123</dc:creator>
      <dc:date>2022-05-04T14:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811475#M320077</link>
      <description>&lt;P&gt;Somewhere in your variables Result_1 to Result_25, you have a mix of numeric and character variables. They must be either all numeric, or all character. So you have to find out which variables are which, and then make them all the same type (all numeric or all character).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, this will cause an error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AResult(25) in ('&amp;lt;0.5','3.8ug/dl','4.0ug/dl','4.1ug/dl','4.2ug/dl','&amp;lt;.3.3','&amp;lt;5','LOW','LOW &amp;lt;3','=1.3')
then AResult{25}=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For AResult(25) to be one of those character strings would mean AResult(25) is character. So you cannot assign a numeric 1 to this variable.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 15:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811475#M320077</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-04T15:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811504#M320090</link>
      <description>Yes, I have mix of numeric and character variables because of the data entry. That is why I am trying to clean it using array and do loop.</description>
      <pubDate>Wed, 04 May 2022 16:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811504#M320090</guid>
      <dc:creator>sasuser123</dc:creator>
      <dc:date>2022-05-04T16:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811508#M320094</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/73681"&gt;@sasuser123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, I have mix of numeric and character variables because of the data entry. That is why I am trying to clean it using array and do loop.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Data entry by itself cannot cause variables to change type.&lt;/P&gt;
&lt;P&gt;Are you saying you used PROC IMPORT to create the data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What was the source of the data?&amp;nbsp; What type of file is it?&lt;/P&gt;
&lt;P&gt;If it is a text file, like a CSV file, then use your own data step to read the text into data and you will have full control over how the variables are defined.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 16:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811508#M320094</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-04T16:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811512#M320098</link>
      <description>&lt;P&gt;I used data step to import.&amp;nbsp; It is txt file.&amp;nbsp; When I specify the variables to be numeric in the data step, then the cells with characters become blank.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 16:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811512#M320098</guid>
      <dc:creator>sasuser123</dc:creator>
      <dc:date>2022-05-04T16:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811513#M320099</link>
      <description>&lt;P&gt;Show us a portion of the .txt file&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 16:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811513#M320099</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-04T16:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811516#M320101</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/73681"&gt;@sasuser123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I used data step to import.&amp;nbsp; It is txt file.&amp;nbsp; When I specify the variables to be numeric in the data step, then the cells with characters become blank.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Of course that is why you need to first read them into character variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;if you want to use them in the same ARRAY then you need to read all of them as character variables, even the ones you think don't have any issues.&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 17:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811516#M320101</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-04T17:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811561#M320124</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/73681"&gt;@sasuser123&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When dealing with "dirty" files is is a good idea to read data as text variables, so everything is read in and nothing lost in automatic conversion to numeric. If the demimiters are in place with no extraneus delimiters in fields, and all variables are supposed to be numeric, I will recommend that all variables are read with informat $char32. and converted to numeric in a following step. That gives full control over the cleaning process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step for reading your example data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.patients;
infile datalines dlm=",";
informat patient_id 8 cResult_1-cResult_3 $char32.;
input patient_id cResult_1-cResult_3 ;
datalines;
1, =70, 65, 82
2, 88, =75, 79ug/dl
3, 64, 72,=80
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When data are read in, all variables can be processed with an array. I have made 3 examples. The first uses the same cleaning algoritm as your code, or rather an algoritm that gives the same result without having to specify the diferent types of non-numeric data. The idea is that if notdigit(variable) is 0, the variable has a contaet that can be converted to numerig, otherwise it is recoded to 1.So four values out of nine in your example data is recoded to 1, while the rest are converted to a numeric variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.result1;
  set work.patients;
  drop cResult_1-cResult_3;
  array cres $ cResult_1-cResult_3;
  array res Result_1-Result_3;
  do i = 1 to 3;
    if notdigit(compress(cres{i},' ')) then res{i} = 1;
    else res{i} = input(left(cres{i}),best32.);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The two other eaxmples are included to demonstrate how a more complicated cleaning algoritm could be applied with a few lines of code. Number two tries to preserve as many values as possible by ignoring extraneus information (i.e. all characters that are not digits) and converting the digits in all nine values to numeric variables. That works only if all digits represent valid values, so e.g. 79ug/dl contains the valid value 79.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Version 3 preserves all values with digits only and an optional wqual sign (=), assuming that the values with the equal sign removed represent valid values, while values with other extraneus information should be recoded to 1. This could be refines further if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.result2;
  set work.patients;
  drop cResult_1-cResult_3;
  array cres $ cResult_1-cResult_3;
  array res Result_1-Result_3;
  do i = 1 to 3;
    res{i} = input(left(compress(cres{i},,'dk')),best32.);
  end;
run;

data work.result3;
  set work.patients;
  drop cResult_1-cResult_3;
  array cres $ cResult_1-cResult_3;
  array res Result_1-Result_3;
  do i = 1 to 3;
    if notdigit(compress(cres{i},' ')) then do;
      if notdigit(compress(cres{i},'= ')) = 0 then res{i} = input(left(compress(cres{i},'=')),best32.);
      else res{i} = 1;
    end;
    else res{i} = input(left(cres{i}),best32.);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 20:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811561#M320124</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-05-04T20:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: All variables in array list must be the same type, i.e., all numeric or character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811575#M320126</link>
      <description>&lt;P&gt;The comparisons are treating the variables as strings, but some of the variables are numeric, so that produces an error. Start with a dataset that includes only variables Result_1 - Result 25. f you specify a _character_ keyword in the array statement like "array c _character_; " the comparisons will work. The numeric variables are already clean, they cannot contain any special characters. If you would like to go over the numeric variables and set anything below 5 to 1, you can specify "array n _numeric_; do over n; if n &amp;lt; 5 then n = 1; end;".&lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 21:52:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/All-variables-in-array-list-must-be-the-same-type-i-e-all/m-p/811575#M320126</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-05-04T21:52:19Z</dc:date>
    </item>
  </channel>
</rss>

