I want to assign a binary code to each column, 1 if it contains information from the first row and 0 otherwise.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str33 WHODISCLOSURE2 str31 WHODISCLOSURE3 str26 WHODISCLOSURE4 str24 WHODISCLOSURE5 "Securities regulator - Encourages" "Securities regulator - Requires" "Corporate law - Encourages" "Corporate law - Requires" "" "Requires" "" "" "" "" "" "" "Encourages" "" "" "" "" "Requires" "" "" "Encourages" "" "" "" "Encourages" "Requires" "" "" "" "Requires" "" "Requires" "Encourages" "" "" "Requires" "" "Requires" "" "" "" "" "" "" "" "Requires" "Encourages" "" "Encourages" "" "" "Requires" "Encourages" "" "" "" "" "Requires" "" "" "" "" "" "" "Encourages" "" "" "Requires" "" "Requires" "Encourages" "" "Encourages" "" "" "" "" "Requires" "" "" "" "" "" "" "Encourages" "" "" "" "" "Requires" "Encourages" "Requires" "" "" "" "" "Encourages" "Requires" "" "" "" "" "" "" "" "Requires" "" "" "" "" "" "" "" "Requires" "" "Requires" "" "" "Encourages" "" "" "Requires" "" "Requires" "Encourages" "Requires" "" "Requires" "" "Requires" "Encourages" "" "" "" "" "" "" "Requires" "" "" "Encourages" "" "" "Requires" "" "Requires" "" "" "Encourages" "Requires" "" "" "" "Requires" "Encourages" "" "" "" "" "" "Encourages" "Requires" "" "" "" "Requires" "" "Requires" "" "" "" "" "" "" "" "" "Encourages" "" "" "" "" "Requires" "" "" "Encourages" "" "Encourages" "" "Encourages" "" "Encourages" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "Encourages" "" "Encourages" "" end
Code:
local N = _N forvalues i = 2/`N'{ foreach j of varlist WHODISCLOSURE* { local m1 = `j'[1] if `j'[`i'] == "`m1'" { replace `j' = "1" in `i' if `j' != "" } replace `j' = "0" in `i' if `j' == "" } }
Many thanks in advance!
0 Response to Identify a segment of a string
Post a Comment