Home > Mobile >  Find corresponding variable to a certain value through array
Find corresponding variable to a certain value through array

Time:09-28

So if I have identified a max value regarding a test result (Highest variable listed below), which occurred during one of the three dates that are being tested (testtime variables listed below), what I want to do is to create a new variable called Highesttime identifying the date when the test was given.

However, I am stuck in an array looping. SAS informs that "ERROR: Array subscript out of range at line x", guess there's something working regarding the logic? See codes below:

Example:

ID time1_a  time_b   time_c result_a result_b result_c Highest 
001 1/1/22  1/2/22   1/3/22     3       2       4        4
002 12/1/21 12/23/21 1/5/22     6       1       2        6
003 12/22/21 1/6/22  2/2/22     5       5       7        7
...

data want;
set origin;
array testtime{3} time1_a time_b time_c;
array maxvalue{1} Highest;
array corr_time{1} Highesttime;
do i=1 to dim(testttime);
corr_time{i}=testttime{i=maxvalue{i}};
end;
run;

CodePudding user response:

There is no need to make an array for HIGHEST since there is only one variable that you would put into that array. In that case just use the variable directly instead of trying to access it indirectly via an array reference.

First let's make an actual SAS dataset out of the listing you provided.

data have;
  input ID (time_a time_b time_c) (:mmddyy.) result_a result_b result_c Highest ;
  format time_a time_b time_c yymmdd10.;
cards;
001 1/1/22  1/2/22   1/3/22     3       2       4        4
002 12/1/21 12/23/21 1/5/22     6       1       2        6
003 12/22/21 1/6/22  2/2/22     5       5       7        7
;

If you want to loop then you need two arrays. One for times and the other for the values. Then you can loop until you find which index points to the highest value and use the same index into the other array.

data want ;
  set have;
  array times time_a time_b time_c ;
  array results result_a result_b result_c;
  do which_one=1 to dim(results) until (not missing(highest_time));
     if results[which_one] = highest then highest_time=times[which_one];
  end;
  format highest_time yymmdd10.;
run;

Or you can avoid the looping by using the WHICHN() function to figure out which of three result variables is the first one that has that HIGHEST value. Then you can use that value as the index into the array of the TIME variables (which in your case have DATE instead of TIME or DATETIME values).

data want ;
  set have;
  which_one = whichn(highest, of result_a result_b result_c);
  array times time_a time_b time_c ;
  highest_time = times[which_one];
  format highest_time yymmdd10.;
run;

enter image description here

CodePudding user response:

Your code from this question was close, you just had the assignment backwards.

Note that an array method will assign the last date in the case of duplicate high results and WHICHN will report the first date so the answers are not identical unless you modify the loop to exit after the first maximum value is found. With the changes suggested in the answer proposed:

data temp2_lead_f2022;
set temp_lead_f2022;
array _day {3} daybld_a daybld_b daybld_c;
array _month {3} mthbld_a mthbld_b mthbld_c;
array _dates {3} date1_a date2_b date3_c;
array _pblev{3} pblev_a pblev_b pblev_c;

do i = 1 to 3;
_dates{i} = mdy(_month{i}, _day{i}, 1990);
end;

maxlead= max(of _pblev(*));


do i=1 to 3;
    if _pblev{i} = maxlead then  max_date=_dates(i);
end;

*Using WHICHN to identify the maximum occurence;
max_first_index=whichn(maxlead, of _pblev(*));
max_date2 = _dates(max_first_index);

drop  k;
format date1_a date2_b date3_c dob mmddyy8. ;

run;

  • Related