REDCapCAST/SAS/REDCap_split.sas

230 lines
6.6 KiB
SAS
Raw Normal View History

2018-01-19 17:50:08 +01:00
/********************************************************************************
*
* FILE: REDCap_split.sas
*
2019-07-26 18:02:20 +02:00
* VERSION: 0.1.0
2018-01-19 17:50:08 +01:00
*
* PURPOSE: Take a REDCap dataset with multiple events and make into several
* tables with primary and foreign keys
*
* AUTHOR: Paul W. Egeler, M.S., GStat
*
* DATE: 22JUN2017
*
*******************************************************************************
*
* INSTRUCTIONS:
*
* 1. Run the SAS code provided by REDCap to load the records into your SAS session.
2018-01-19 17:50:08 +01:00
*
2018-05-25 18:02:42 +02:00
* 2. Download the data dictionary for your project.
2018-05-29 05:47:16 +02:00
*
2018-05-25 18:02:42 +02:00
* 3. Run the macro definitions REDCAP_READ_DATA_DICT and REDCAP_SPLIT
*
2018-05-29 05:47:16 +02:00
* 4. Run the macro call for REDCAP_READ_DATA_DICT to load in the data dictionry.
2018-05-25 18:02:42 +02:00
* This is necessary to split the tables correctly.
2018-01-19 17:50:08 +01:00
*
2018-05-29 05:47:16 +02:00
* 5. Run the macro call for REDCAP_SPLIT. You will have an output dataset for
2018-05-25 18:02:42 +02:00
* your main table as well as for each repeating instrument.
2018-01-19 17:50:08 +01:00
*
********************************************************************************/
2018-05-25 18:02:42 +02:00
%MACRO REDCAP_READ_DATA_DICT(
DATA_DICTIONARY /* The file path for the data dictionary */
);
DATA REDCAP_DATA_DICTIONARY;
LENGTH VAR_NAME $ 200 FORM_NAME $ 200 SECTION_HEADER $ 200 FIELD_TYPE $ 200 X1-X14 $ 2250;
INFILE "&DATA_DICTIONARY" FIRSTOBS = 2 DSD DLM = "," LRECL=32767;
INPUT VAR_NAME $ FORM_NAME $ SECTION_HEADER $ FIELD_TYPE $ X1-X14 $;
IF FIELD_TYPE EQ "descriptive" THEN DELETE;
DROP SECTION_HEADER X1-X14;
2018-05-25 18:02:42 +02:00
RUN;
%MEND REDCAP_READ_DATA_DICT;
2018-01-19 17:50:08 +01:00
%MACRO REDCAP_SPLIT(
2018-05-29 05:47:16 +02:00
DATA_SET = REDCAP /* The name of the SAS dataset created by REDCap */,
2019-07-26 18:02:20 +02:00
DATA_DICTIONARY = REDCAP_DATA_DICTIONARY /* The name of the SAS dataset of the data dictionary */,
2019-07-26 20:48:12 +02:00
NUMERIC_SUBTABLES = N /* Y/N: Should the subtables be numbered (Y) or should they be based on the name of the repeating instrument (N)? */
2018-01-19 17:50:08 +01:00
);
2019-07-26 18:02:20 +02:00
/* Find the key that links the base table to child tables */
/* Also check that REDCAP_REPEAT_INSTRUMENT is present in the data */
%LET DSID = %SYSFUNC(OPEN(&DATA_SET));
%LET KEY = %SYSFUNC(VARNAME(&DSID, 1));
%LET CHECK = %SYSFUNC(VARNUM(&DSID, REDCAP_REPEAT_INSTRUMENT));
%LET RC = %SYSFUNC(CLOSE(&DSID));
%IF &CHECK EQ 0 %THEN %DO;
%PUT ERROR: The dataset &DATA_SET does not contain repeating instruments.;
%PUT ERROR: Stopping MACRO.;
%GOTO FINISH;
%END;
/* Remove formatting from repeat instrument field */
2019-07-26 18:02:20 +02:00
%PUT NOTE: Removing formatting from REDCAP_REPEAT_INSTRUMENT in &DATA_SET..;
DATA &DATA_SET.;
SET &DATA_SET.;
FORMAT REDCAP_REPEAT_INSTRUMENT;
RUN;
/* Find the subtable names and number of subtables */
2018-05-25 18:02:42 +02:00
PROC SQL NOPRINT;
SELECT DISTINCT
REDCAP_REPEAT_INSTRUMENT,
"'"!!trim(REDCAP_REPEAT_INSTRUMENT)!!"'" AS INSTRUMENT_QUOTED
INTO
:INSTRUMENTS SEPARATED BY ' ',
:INSTRUMENT_LIST SEPARATED BY ','
FROM &DATA_SET AS A
WHERE REDCAP_REPEAT_INSTRUMENT GT '';
%LET N_SUBTABLES = &SQLOBS;
QUIT;
2019-07-26 18:02:20 +02:00
%IF &N_SUBTABLES EQ 0 %THEN %DO;
2019-07-26 18:02:20 +02:00
%PUT WARNING: There were no records containing repeating instruments in the dataset %LEFT(&DATA_SET).;
%PUT WARNING: No action was taken.;
%GOTO FINISH;
%END;
%PUT N SUBTABLES: %LEFT(&N_SUBTABLES);
%PUT INSTRUMENTS: %LEFT(&INSTRUMENTS);
%PUT INSTRUMENT LIST: %LEFT(%BQUOTE(&INSTRUMENT_LIST));
/* Get information on the variables in the dataset */
PROC CONTENTS
DATA = &DATA_SET.
OUT = REDCAP_VARNAMES(KEEP=NAME VARNUM)
NOPRINT;
RUN;
/* Make a list of fields and their associated forms based on data dictionary */
DATA REDCAP_FIELDS(KEEP=VAR_NAME FORM_NAME);
SET &DATA_DICTIONARY.;
IF FIELD_TYPE EQ "checkbox" THEN DO;
BASENAME = VAR_NAME;
DO I = 1 TO N;
SET REDCAP_VARNAMES POINT=I NOBS=N;
IF PRXMATCH("/^"!!trim(BASENAME)!!"___.+$/", NAME) THEN DO;
VAR_NAME = NAME;
OUTPUT;
END;
2019-07-26 18:02:20 +02:00
END;
END;
ELSE OUTPUT;
RUN;
2019-07-26 18:02:20 +02:00
/* Add instrument status fields to list of fields */
PROC SQL;
CREATE TABLE REDCAP_INSTRUMENT_STATUS_FIELDS AS
2019-07-26 18:02:20 +02:00
SELECT DISTINCT TRIM(FORM_NAME)!!"_complete" AS VAR_NAME LENGTH=200, FORM_NAME
FROM &DATA_DICTIONARY.;
2019-07-26 18:02:20 +02:00
QUIT;
2019-07-26 18:02:20 +02:00
PROC APPEND
BASE=REDCAP_FIELDS
DATA=REDCAP_INSTRUMENT_STATUS_FIELDS;
RUN;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
DATA REDCAP_FIELDS;
SET REDCAP_FIELDS;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
IF LENGTH(VAR_NAME) GT 32 THEN DO;
PUT "WARNING: The variable " VAR_NAME "is too long (MAX 32 CHARACTERS).";
PUT "WARNING: " VAR_NAME "will not be included in the output dataset.";
DELETE;
END;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
RUN;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
/* Sort out the field names */
PROC SQL NOPRINT;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
SELECT VAR_NAME
INTO :VARS_BASE SEPARATED BY ' '
FROM REDCAP_FIELDS AS A
WHERE FORM_NAME NOT IN (&INSTRUMENT_LIST);
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
%put Base vars: &VARS_BASE;
2018-05-25 18:02:42 +02:00
%DO I = 1 %TO &N_SUBTABLES;
%LET INSTRUMENT_I = %SCAN(&INSTRUMENTS,&I,%STR( ));
2019-07-26 18:02:20 +02:00
SELECT VAR_NAME
INTO :VARS_&I. SEPARATED BY ' '
FROM REDCAP_FIELDS AS A
WHERE FORM_NAME EQ "&INSTRUMENT_I.";
%PUT &INSTRUMENT_I. vars: &&VARS_&I;
2018-05-25 18:02:42 +02:00
%END;
2019-07-26 18:02:20 +02:00
QUIT;
/* Make new data sets based on field names above */
DATA &DATA_SET._BASE (KEEP = &VARS_BASE);
SET &DATA_SET;
IF MISSING(REDCAP_REPEAT_INSTRUMENT);
RUN;
2019-07-26 18:02:20 +02:00
%DO I = 1 %TO &N_SUBTABLES;
%LET INSTRUMENT_I = %SCAN(&INSTRUMENTS,&I,%STR( ));
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
/* Create either numeric table name or name based on repeating instrument name (see issue #12) */
%IF &NUMERIC_SUBTABLES EQ N %THEN %DO;
2018-05-25 18:02:42 +02:00
2019-07-26 18:02:20 +02:00
%LET SUBTABLE_NAME = &DATA_SET._&INSTRUMENT_I.;
%IF %LENGTH(&SUBTABLE_NAME) GT 32 %THEN %DO;
%PUT ERROR: The table name &SUBTABLE_NAME is %LENGTH(&SUBTABLE_NAME) characters long (MAX 32 CHARACTERS).;
%PUT ERROR: &SUBTABLE_NAME will be skipped. Consider setting NUMERIC_SUBTABLES = Y.;
%GOTO SKIP;
%END;
%END;
%ELSE %DO;
%LET SUBTABLE_NAME = &DATA_SET._&I.;
%END;
DATA &SUBTABLE_NAME (KEEP = &KEY redcap_repeat_instance &&VARS_&I);
SET &DATA_SET;
IF REDCAP_REPEAT_INSTRUMENT EQ "&INSTRUMENT_I.";
RUN;
%PUT NOTE: Records from instrument &INSTRUMENT_I have been placed in &SUBTABLE_NAME;
%SKIP:
2018-05-25 18:02:42 +02:00
%END;
2018-01-19 17:50:08 +01:00
2019-07-26 18:02:20 +02:00
/* Clean up temporary datasets */
PROC DATASETS MEMTYPE=DATA LIBRARY=WORK NOLIST;
DELETE REDCAP_FIELDS REDCAP_VARNAMES REDCAP_INSTRUMENT_STATUS_FIELDS;
RUN;
%FINISH:
2018-01-19 17:50:08 +01:00
%MEND REDCAP_SPLIT;