Loading REDCap Repeated Instruments into SAS
What is the issue?
REDCap allows you to repeatedly measure people with multiple instruments (forms/questionnaires) in a single project. The pattern of who gets administered what and when can get truly complex. If you have a complex schedule of instrument administration, where an instrument is only given on a scattering of assessment days, your export will be full of holes for the visits where that instrument was not given.
The code below can be used to take a SAS dataset created by REDCap and put the data from an instrument into its own table (without the blank records for visits where the instrument was not used.)
%macro import(table /* new table name */,
first, /* first variable on the instrument/form */,
last /* first variable on the instrument/form */);
data &table. (drop = x hasSome);
set redcap (keep = record_id redcap_event_name &first. -- &last.);
array thing _numeric_;
array thingC _character_;
do x = 1 to dim(thing);
if not missing(thing[x]) then hasSome = 1;
end;
do x = 3 to dim(thingC);
if not missing(thingC[x]) then hasSome = 1;
end;
id = input(record_id, 8.);
if hasSome then output &table.;
run;
%mend;
To use the macro, call it with three arguments:
- the name of the new table
- the name of the first variable on a REDCap instrument
- the name of the last variable for that instrument
For example, %import(demographics_source, demo_birth_year, demo_country_other);
makes a demographics table in the work library which is holding the variables starting with the date of birth out through the variable holding other countries.