<?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: Indexing a variable for values specified within an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944505#M370060</link>
    <description>&lt;P&gt;If you want to search an array for a specific variable then WHICHC (or Whichn for numeric values) may be what you are looking for. The function returns the&amp;nbsp; position number if a value is found or 0 otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
  input Sample_time_point $;
  array raw_tpts {6} $200. _temporary_ ('PRE' '1HR' '2HR' '4HR' '24HR' '96HR');
  position = whichc(Sample_time_point, of raw_tpts(*));
datalines;
ABC        /* a value not in your list*/
PRE
1HR
2HR
4HR
24HR
96HR
;&lt;/PRE&gt;
&lt;P&gt;So find the position and then use something like:&lt;/P&gt;
&lt;PRE&gt;if position &amp;gt;0 then do;
   PCTPT = map_tpts[position];
   PCTPTNUM = tptnums[position];
end;&lt;/PRE&gt;
&lt;P&gt;Though if the values in the three arrays raw_tpts, tptnums and map_tpts arrays don't change frequently I would be tempted to create custom informat to read the text of Sample_time_point (or any other variable holding that text) into a numberic value of 1 to 6 and formats for the values 1 to 6 for the other two and use something like:&lt;/P&gt;
&lt;PRE&gt;   PCTPT = Put(position,map_tpts.);
   PCTPTNUM = Put(position,tptnums.) ;&lt;/PRE&gt;
&lt;P&gt;Just define the format to return a blank for position=0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example of custom reading text into a numeric value:&lt;/P&gt;
&lt;PRE&gt;Proc format library=work;
invalue raw_tpts
'PRE'=1
'1HR'=2
'2HR'=3
'4HR'=4
'24HR'=5
'96HR'=6
other = .
;
run;

data example2;
  input position raw_tpts.;
datalines;
ABC        
PRE
1HR
2HR
4HR
24HR
96HR
;

&lt;/PRE&gt;
&lt;P&gt;I use lots of custom informats and corresponding formats to check on values and display meaningful text for code values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you could use the OTHER=_error_ in the informat to report invalid data if it isn't supposed to be missing or have other values than the 6 listed.&lt;/P&gt;</description>
    <pubDate>Wed, 18 Sep 2024 20:50:07 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-09-18T20:50:07Z</dc:date>
    <item>
      <title>Indexing a variable for values specified within an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944501#M370058</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to search a variable for a list of strings, which I have specified in an array. My initial thought was to use an index() statement, but I have also tried find() statements as well. I am trying to do something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;array raw_tpts {6} $200. _temporary_ ('PRE' '1HR' '2HR' '4HR' '24HR' '96HR');&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;array map_tpts {6} $200. _temporary_ ('PRE-DOSE' '1 HOUR POST-DOSE' '2 HOURS POST-DOSE' '4 HOURS POST-DOSE'&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt; '24 HOURS POST-DOSE' '96 HOURS POST-DOSE');&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;array tptnums {6} 8. _temporary_ (0 1 2 4 24 96);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;do i=1 to 6;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if index(SAMPLE_TIME_POINT, raw_tpts[i]) &amp;gt;0 then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PCTPT = map_tpts[i];&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PCTPTNUM = tptnums[i];&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 18 Sep 2024 20:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944501#M370058</guid>
      <dc:creator>kmikkelson2</dc:creator>
      <dc:date>2024-09-18T20:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a variable for values specified within an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944505#M370060</link>
      <description>&lt;P&gt;If you want to search an array for a specific variable then WHICHC (or Whichn for numeric values) may be what you are looking for. The function returns the&amp;nbsp; position number if a value is found or 0 otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
  input Sample_time_point $;
  array raw_tpts {6} $200. _temporary_ ('PRE' '1HR' '2HR' '4HR' '24HR' '96HR');
  position = whichc(Sample_time_point, of raw_tpts(*));
datalines;
ABC        /* a value not in your list*/
PRE
1HR
2HR
4HR
24HR
96HR
;&lt;/PRE&gt;
&lt;P&gt;So find the position and then use something like:&lt;/P&gt;
&lt;PRE&gt;if position &amp;gt;0 then do;
   PCTPT = map_tpts[position];
   PCTPTNUM = tptnums[position];
end;&lt;/PRE&gt;
&lt;P&gt;Though if the values in the three arrays raw_tpts, tptnums and map_tpts arrays don't change frequently I would be tempted to create custom informat to read the text of Sample_time_point (or any other variable holding that text) into a numberic value of 1 to 6 and formats for the values 1 to 6 for the other two and use something like:&lt;/P&gt;
&lt;PRE&gt;   PCTPT = Put(position,map_tpts.);
   PCTPTNUM = Put(position,tptnums.) ;&lt;/PRE&gt;
&lt;P&gt;Just define the format to return a blank for position=0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example of custom reading text into a numeric value:&lt;/P&gt;
&lt;PRE&gt;Proc format library=work;
invalue raw_tpts
'PRE'=1
'1HR'=2
'2HR'=3
'4HR'=4
'24HR'=5
'96HR'=6
other = .
;
run;

data example2;
  input position raw_tpts.;
datalines;
ABC        
PRE
1HR
2HR
4HR
24HR
96HR
;

&lt;/PRE&gt;
&lt;P&gt;I use lots of custom informats and corresponding formats to check on values and display meaningful text for code values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you could use the OTHER=_error_ in the informat to report invalid data if it isn't supposed to be missing or have other values than the 6 listed.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 20:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944505#M370060</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-18T20:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a variable for values specified within an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944580#M370083</link>
      <description>&lt;P&gt;If your source variable&amp;nbsp;&lt;SPAN&gt;SAMPLE_TIME_POINT contains data like in below sample Have table and you can match against the full string then code like below could work.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input SAMPLE_TIME_POINT $;
  datalines;
PRE 
1HR 
2HR 
4HR 
24HR 
96HR
;

proc format;
  value $map_tpts
    'PRE' = 'PRE-DOSE' 
    '1HR' = '1 HOUR POST-DOSE' 
    '2HR' = '2 HOURS POST-DOSE' 
    '4HR' = '4 HOURS POST-DOSE' 
    '24HR'= '24 HOURS POST-DOSE'
    '96HR'= '96 HOURS POST-DOSE'
    ;
  value $tptnums
    'PRE' = '0'
    '1HR' = '1'
    '2HR' = '2'
    '4HR' = '3'
    '24HR'= '24'
    '96HR'= '96'
    ;
run;

data want;
  set have;
  PCTPT=put(SAMPLE_TIME_POINT,$map_tpts.);
  PCTPTNUM=put(SAMPLE_TIME_POINT,$tptnums.);
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2024 11:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-variable-for-values-specified-within-an-array/m-p/944580#M370083</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-19T11:30:51Z</dc:date>
    </item>
  </channel>
</rss>

