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:
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.