BookmarkSubscribeRSS Feed
kmikkelson2
Calcite | Level 5

Hi,

 

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:

 

 

array raw_tpts {6} $200. _temporary_ ('PRE' '1HR' '2HR' '4HR' '24HR' '96HR');
 
array map_tpts {6} $200. _temporary_ ('PRE-DOSE' '1 HOUR POST-DOSE' '2 HOURS POST-DOSE' '4 HOURS POST-DOSE'  '24 HOURS POST-DOSE' '96 HOURS POST-DOSE');
 
array tptnums {6} 8. _temporary_ (0 1 2 4 24 96);
 
do i=1 to 6;
    if index(SAMPLE_TIME_POINT, raw_tpts[i]) >0 then do;
          PCTPT = map_tpts[i];
          PCTPTNUM = tptnums[i];
    end;
end;
2 REPLIES 2
ballardw
Super User

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  position number if a value is found or 0 otherwise.

 

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
;

So find the position and then use something like:

if position >0 then do;
   PCTPT = map_tpts[position];
   PCTPTNUM = tptnums[position];
end;

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:

   PCTPT = Put(position,map_tpts.);
   PCTPTNUM = Put(position,tptnums.) ;

Just define the format to return a blank for position=0.

 

An example of custom reading text into a numeric value:

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
;

I use lots of custom informats and corresponding formats to check on values and display meaningful text for code values.

 

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.

Patrick
Opal | Level 21

If your source variable 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.

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;
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1006 views
  • 2 likes
  • 3 in conversation