| ID | _n1 | _n2 | _n3 | category |
| 67 | 889 | 192 | 78 | |
| 78 | ||||
| 101 | ||||
| 192 | ||||
| 203 | 724 | 538 | 889 | |
| 538 | ||||
| 724 | ||||
| 889 |
It should look like this:
| ID | _n1 | _n2 | _n3 | category |
| 67 | 889 | 192 | 78 | 2 |
| 78 | 1 | |||
| 101 | 0 | |||
| 192 | 1 | |||
| 203 | 724 | 538 | 889 | 2 |
| 538 | 1 | |||
| 724 | 1 | |||
| 889 | 1 |
I will appreciate your help.
| ID | _n1 | _n2 | _n3 | category |
| 67 | 889 | 192 | 78 | |
| 78 | ||||
| 101 | ||||
| 192 | ||||
| 203 | 724 | 538 | 889 | |
| 538 | ||||
| 724 | ||||
| 889 |
| ID | _n1 | _n2 | _n3 | category |
| 67 | 889 | 192 | 78 | 2 |
| 78 | 1 | |||
| 101 | 0 | |||
| 192 | 1 | |||
| 203 | 724 | 538 | 889 | 2 |
| 538 | 1 | |||
| 724 | 1 | |||
| 889 | 1 |
* Example generated by -dataex-. For more info, type help dataex
clear
input int(id _n1 _n2 _n3) byte category
67 889 192 78 2
78 . . . 1
101 . . . 0
192 . . . 1
203 724 538 889 2
538 . . . 1
724 . . . 1
889 . . . 1
end
forval i=1/3{
qui levelsof _n`i', local(levs`i') sep(|)
}
local levs `levs1'|`levs2'|`levs3'
gen wanted=cond(!missing(_n3), 2, cond(regexm(trim(string(id, "%9.0f")), "(`levs')"), 1, 0))
. l, sep(0)
+-------------------------------------------+
| id _n1 _n2 _n3 category wanted |
|-------------------------------------------|
1. | 67 889 192 78 2 2 |
2. | 78 . . . 1 1 |
3. | 101 . . . 0 0 |
4. | 192 . . . 1 1 |
5. | 203 724 538 889 2 2 |
6. | 538 . . . 1 1 |
7. | 724 . . . 1 1 |
8. | 889 . . . 1 1 |
+-------------------------------------------+
Comment