J.01
J.01

Reputation: 1

replace string variable

In Stata, I am trying to replace a parliamentary party group variable PPG with a certain type, e.g., EEP if the party names belong to that party group at this time. The issue seems to be the string variable (the partyname), since every time I copy the name into the command, Stata does not seem to recognise the value and always states '0 changes made'. I have trimmed the variable with strtrim so blanks cannot be the reason for this.

After the following:

replace partyname = strtrim(partyname)

tab partyname
codebook partyname, tab(1000)

I get party names with two "" "" on each side, maybe that's the issue? See here: codebook result

I then proceed (just as an example with one party here):

gen PPG = "NA"
label var PPG "Parliamentary Party Groups of the EP" 

replace PPG = "EEP-ED" if partyname == "Österreichische Volkspartei" & date < td(20jul2004)

and Stata then says: 'O changes made.'

Encoding this does not solve the issue but rather makes it more complicated.

Upvotes: 0

Views: 488

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

replace PPG = "EEP-ED" if strpos(partyname, "Österreichische Volkspartei") & date < td(20jul2004)

would test for the occurrence of that text and so ignore any characters before and after.

The trim functions remove simple spaces, but they won't remove bounding quotation marks that somehow have been included in a string.

I mention for the future a more puzzling detail. They won't remove either certain ASCII characters that look like spaces. ASCII 160 is an example.

. clear

. set obs 1
Number of observations (_N) was 0, now 1.

. gen test =  uchar(160) + "frog" + uchar(160)

. di ("|" + test[1] + "|")
| frog |


. l

     +--------+
     |   test |
     |--------|
  1. |  frog  |
     +--------+

. replace test = strtrim(test)
(0 real changes made)

See chartab from SSC as a way of documenting what characters appear in your string variable.

Upvotes: 0

Related Questions