REDCap Help TIP:  Some older code examples have the token placed at the end of the call, however the token should be passed first. This should allow you to pull a flat file for REDCap into SAS.   


/*****************************************************************
Step 1. Define file names and macro variable for the
project-specific t oken.
*******************************************************************/
*** Text file for API parameters that the define the request sent to
REDCap API. Will be created in a DATA step. Extension can be .csv,
.txt, .dat ***;
filename my_in "C:\cchmc_alka\redcap\redcapapi_parameter.txt";
*** .CSV output file to contain the exported data Please change this to the location and file name you would need***;
filename my_out "C:\cchmc_alka\redcap\redcap_data.csv";
*** Output file to contain PROC HTTP status information returned from REDCap API (this is optional) ***;
**filename status "C:\cchmc_alka\redcap\redcap_status.txt";
*** Project- and user-specific token obtained from REDCap Add the API token for the user running the program***;
%let mytoken =*************************************;

*** To delete existing files from folder. Please create a copy incase you need to save old files ***;
data _null_ ;
rc=filename(my_out,"physical-filename");
if rc = 0 and fexist(my_out) then
rc=fdelete(my_out);
rc=filename(my_out);
run;

data _null_ ;
rc=filename(my_in,"physical-filename");
if rc = 0 and fexist(my_in) then
rc=fdelete(my_in);
rc=filename(my_in);
run;

/**********************************************************
Step 2. Request all observations (CONTENT=RECORDS) with one
row per record (TYPE=FLAT). Note: Obtain your site-specific
url from your local REDCap support team.
******************************************************/
*** Create the text file to hold the API parameters. ***;
data _null_ ;
file my_in ;
put "%NRStr(token=)&mytoken%NRStr(&content=record&type=flat&format=csv)&";
run;
*** PROC HTTP call. Everything except HEADEROUT= is required. ***;
proc http
in= my_in
out= my_out
/*headerout = status*/
url ="https://redcap.research.cchmc.org/api/"
method="post";
run;
/************************** CAUTION ***************************/
/* */
/* This program UPDATES IN PLACE, create a backup copy before */
/* running. */
/* */
/************************** CAUTION ***************************/

/* Replace carriage return and linefeed characters inside */
/* double quotes with a specified character. CR/LFs not in double quotes will not be replaced. */

%let repA=' '; /* replacement character LF */
%let repD=' '; /* replacement character CR */


**%let dsnnme="c:\sample.csv"; /* use full path of CSV file */
data _null_;
/* RECFM=N reads the file in binary format. The file consists */
/* of a stream of bytes with no record boundaries. SHAREBUFFERS */
/* specifies that the FILE statement and the INFILE statement */
/* share the same buffer. */
infile my_out recfm=n sharebuffers;
file my_out recfm=n;

/* OPEN is a flag variable used to determine if the CR/LF is within */
/* double quotes or not. Retain this value. */
retain open 0;

input a $char1.;
/* If the character is a double quote, set OPEN to its opposite value. */
if a = '"' then open = ^(open);

/* If the CR or LF is after an open double quote, replace the byte with */
/* the appropriate value. */
if open then do;
if a = '0D'x then put &repD;
else if a = '0A'x then put &repA;
end;
run;

  • No labels