Search code examples
sas

SAS: create new variables from array


I have a dataset with 20 variables that all start with date_, that represent the date of a particular procedure. I wish to use an arrays and do to create 20 new variables that have the same name, except the date_ is replaced with time_, and is equal to the number of days between the date_ variable and some reference date ref_date.

So far, I can perform the very simple operation to replace each element:

data morbidity;
    set morbidity;

    array dates date_:;

    do over dates;
        dates =  dates - ref_date;
    end;
    format date_: 11.;
run;

But I can't figure out how to specify a new array times with the same suffix as dates:

data morbidity;
    set morbidity;

    array dates date_:;
    array times /* Some array here */

    do over dates;
        times=  dates - ref_date;
    end;
run;

So, take for example date_amputation, I wish to create time_amputation which is equal to the difference between date_amputation and ref_date.


Solution

  • You need to know how many variables you want to create.

    So code like this will work for up to 20 new variables. If you do not list the variable names in the ARRAY statement then they will be named TIMES1 to TIMES20.

    data morbidity;
        set morbidity;
    
        array dates date_:;
        array times [20] ;
    
        do index=1 to min(dim(dates),dim(times);
           times[index]=  dates[index] - ref_date;
        end;
        drop index;
    run;
    

    If you want the new variable names to be based on the old variable names then you will need to use some code generation.

    proc sql noprint;
      select name,'time'||substr(name,5)
        into :dates separated by ' '
           , :times separated by ' '
        from dictionary.columns
        where libname='WORK' and memname='MORBIDITY'
          and upcase(name) like 'DATE^_%' escape '^'
       ;
    quit
    
    data morbidity;
        set morbidity;
    
        array dates &dates;
        array times ×
    
        do index=1 to dim(dates);
           times[index]=  dates[index] - ref_date;
        end;
        drop index;
    run;