-
Notifications
You must be signed in to change notification settings - Fork 1
/
grepstr.ado
27 lines (26 loc) · 859 Bytes
/
grepstr.ado
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
********************************************************************************
* NAME: grepstr.ado
* DESCRIPTION: grep search on string variable
* AUTHOR: Keith Finlay
* LAST UPDATED: 26 September 2014
********************************************************************************
program define grepstr
version 13.1
syntax varname(str) [if] [in], pattern(string asis) matchgen(name) [markgen(name) grepopts(string asis)]
marksample touse
tempvar matchtemp
gen `matchtemp' = ""
tempfile grepin grepout
forval i=1/`=_N' {
if `touse'[`i']==1 {
outfile `varname' using `grepin' in `i', runtogether replace
shell grep `grepopts' '`pattern'' "`grepin'" > `grepout'
replace `matchtemp' = fileread("`grepout'") in `i'
rm `grepout'
}
}
gen `matchgen' = `matchtemp'
if strlen("`markgen'") {
gen `markgen'=strlen(`matchtemp')!=0
}
end