-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path9_Delete_Records.do
76 lines (60 loc) · 1.99 KB
/
9_Delete_Records.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
Using the REDCap API with Stata
Luke Stevens, Murdoch Childrens Research Institute
10-Apr-2018
9. Delete_Records.do
An example of deleting multiple records.
Nb. The user must have the "Delete Records" permission enabled.
*/
version 12
set more off
clear
local curlpath="c:\curl\curl.exe"
local apiurl="https://redcap.mcri.edu.au/api/"
local tempfile "tempdata.csv"
local token "<insert your token here>"
*_______________________________________________________________________________
* first generate and import a few records that we can afterwards delete
** find the name of the record_id field
local cmd="`curlpath'" ///
+ " --form token=`token'" ///
+ " --form content=metadata" ///
+ " --form format=csv" ///
+ " --output `tempfile'" ///
+ " `apiurl'"
shell `cmd'
import delimited `tempfile'
local pkfield=field_name in 1
clear
** generate some test records and import
gen str11 `pkfield'=""
local recidstem="DELETETEST"
local numrecs=4
set obs `numrecs'
forvalues i=1/`numrecs' {
replace `pkfield'="`recidstem'"+string(`i') in `i'
}
export delimited using `tempfile', nolabel replace
local cmd="`curlpath'" ///
+ " --form token=`token'" ///
+ " --form content=record" ///
+ " --form format=csv" ///
+ " --form type=flat" ///
+ " --form data='<`tempfile'' `apiurl'"
shell `cmd'
shell rm `tempfile'
*_______________________________________________________________________________
* now build the delete command
local recordlist
forvalues i=1/`numrecs' {
local recordlist="`recordlist' --form records[]=`recidstem'"+string(`i')
}
local cmd="`curlpath'" ///
+ " --form token=`token'" ///
+ " --form content=record" ///
+ " --form action=delete" ///
+ " `recordlist' " ///
+ " `apiurl'"
shell `cmd'
* now check your project's Logging page...
*_______________________________________________________________________________