Search code examples
sasdatastep

SAS data step - dynamic select field from table


I have little problem with one case. Please look at table HAVE. And WANT. My process: get value from VAR, divide by 12, round up to the nearest whole number and put in WANT table, in RES field value from FIELD&"result of the action". How I can do that? My final table has thousand records, and these fields hundred - by two kind.

DATA HAVE;
infile DATALINES dsd missover;
input ID VAR FIELD1 FIELD2 FIELD3 FIELD4;
CARDS;
1, 10, 1, 6, 5, 6
2, 20, 1, 6, 8, 8
3, 30, 5, 8, 12, 7
4, 40, 5, 9, 5, 6
5, 50, 8, 10, 12, 8
;run;

DATA WANT;
infile DATALINES dsd missover;
input ID RES;
CARDS;
1, 1
2, 6
3, 12
4, 6
5, 8
;run;

THANK YOU!!


Solution

  • Looks like you want to use an ARRAY statement. This will allow you reference a variable by an index into the array.

    data want;
      set have ;
      array f field1-field4;
      index=ciel(var/12);
      if 1 <= index <= dim(f) then res=f[index];
    run;