Dear Statalisters,
Apologies in advance if this thread contains any English errors. I am glad to join this forum.
Let's suppose I have a variable named var that is coded according to the classification oldclass at the 4-digits level for individuals. I would like to convert var's values into the latest classification newclass at the 2-digits level, but as some of you may know, the mapping between two classifications isn't always clear-cut as there are codes in oldclass that are split into many codes in newclass and vice-versa, even considering the two-digits level.
The concordance table I have at my disposal provides a way to deal with this issue. Please have a look at a sample of the table:
As you can see, at the 2-digits level, there is nothing to be concerned about: even if there are changes at the 4-digits level, ultimately var would be coded 01 in both cases. But consider a situation where a value of oldclass gets split into two parts in newclass and those two parts do not have the same first two digits, as in the following example:
Here's where it gets complicated. In my database, I have a variable named details that gives information about the individual, in addition to his oldclass value. To understand what will be his new newclass value, I must use the variable key displayed above: it contains words like keywordnumber1 or keyword2 that can also be found in the variable details. If a keyword of the variable key matches a word of the variable details, then the individual's new value at the 2-digits level in newclass will be given by the variable repl1. Otherwise, if there's no match, then I should give the individual the value given by the variable repl2 displayed above.
Forgive me if it's unclear, but if you understood well, my request is simple: I would like to find a piece of code to automatically search for certain keywords (maybe those would be stored in a global that would be looped over?) in the variable details, and then, if there is a match, to create a variable newclass_2digits that is equal to the final value of the individual's var according to the newclass classification.
Please note that I can destring or extract a substring of every variable displayed so far if needed. One solution could be to look for matches by hand, but it seems very long and I am worried I can do mistakes caused by inattention. The tricky part is that each key value can have several keywords in it, each separated by a comma, and I need to have this procedure for each keyword individually.
For instance, if individual X belonged in code 1551 in oldvar as in the example above, and if her details variable (a string variable that is used to write additional info) matches EITHER keywordnumber1 OR keyword2, or BOTH, then her newclass_2digits should be 20 according to repl1. If there's no match, it should be repl2's number, i.e. 11!
Thanks a lot for any help that would make me do this correspondance faster than a line-by-line method!
Apologies in advance if this thread contains any English errors. I am glad to join this forum.
Let's suppose I have a variable named var that is coded according to the classification oldclass at the 4-digits level for individuals. I would like to convert var's values into the latest classification newclass at the 2-digits level, but as some of you may know, the mapping between two classifications isn't always clear-cut as there are codes in oldclass that are split into many codes in newclass and vice-versa, even considering the two-digits level.
The concordance table I have at my disposal provides a way to deal with this issue. Please have a look at a sample of the table:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str4(oldclass newclass) str33 key byte(repl1 repl2) "0111" "0115" "" . . "0111" "0119" "" . . "0111" "0129" "" . . "0111" "0112" "" . . "0111" "0116" "" . . "0111" "0113" "" . . "0111" "0128" "" . . "0111" "0163" "" . . "0111" "0114" "" . . "0111" "0126" "" . . end
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str4(oldclass newclass) str33 key byte(repl1 repl2) "1551" "2011" "keywordnumber1, keyword2" 20 11 "1551" "1101" "" . 11 end
Forgive me if it's unclear, but if you understood well, my request is simple: I would like to find a piece of code to automatically search for certain keywords (maybe those would be stored in a global that would be looped over?) in the variable details, and then, if there is a match, to create a variable newclass_2digits that is equal to the final value of the individual's var according to the newclass classification.
Please note that I can destring or extract a substring of every variable displayed so far if needed. One solution could be to look for matches by hand, but it seems very long and I am worried I can do mistakes caused by inattention. The tricky part is that each key value can have several keywords in it, each separated by a comma, and I need to have this procedure for each keyword individually.
For instance, if individual X belonged in code 1551 in oldvar as in the example above, and if her details variable (a string variable that is used to write additional info) matches EITHER keywordnumber1 OR keyword2, or BOTH, then her newclass_2digits should be 20 according to repl1. If there's no match, it should be repl2's number, i.e. 11!
Thanks a lot for any help that would make me do this correspondance faster than a line-by-line method!
Comment