Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Short cut for repeat code, replacing just string value. Also

    Two questions:

    1. My main question is, Can I create a code short cut to replace the string variables within a set code.

    I am analysing survey data. Questions where multiple responses are allowed, the survey system exports all the responses in one cell separated by a comma. I've split these in Stata using.

    'split variable, p(,)'
    Now to count/convert each instance I've started with this code.

    generate variable2=1 if splitvarialble1=="stringvalueA" | splitvarialble2=="stringvalueA" | splitvarialble3=="stringvalueA" etc...
    generate variable3=1 if splitvarialble1=="stringvalueB" | splitvarialble2=="stringvalueB" | splitvarialble3=="stringvalueB" etc...
    generate variable4=1 if splitvarialble1=="stringvalueC" | splitvarialble2=="stringvalueC" | splitvarialble3=="stringvalueC" etc...
    This has essentially counted the instances into a numeric value in a new column.

    Click image for larger version

Name:	Picture2.jpg
Views:	1
Size:	36.9 KB
ID:	1756040


    However, I have to replace the string variable in the code each time by hand. Is there a way to code this more simply or less copy past. I have a list of the string values (they are a fixed list from the survey).


    Is it possible, or worth it, to arrange these up as a sequence within one cell eg. [0,1,1,1,0,0,0,1,0,1,1], where somewhere the string values are encoded to each position within the set (I hope this makes sense).

    Many thanks,

    Dr. DB

  • #2
    I cannot follow your post, and I suspect that others will have problems doing so as well. I recommend that you start with a data example (use dataex to present one), showing "this is what I have now" and "this is where I want to get to," rather than describing your attempted solution to the problem.

    Comment


    • #3

      I am embarrassed to report how long it took me to see that

      Code:
      gen anyA = inlist("A", var1, var2, var3)
      is a way to do part of what I guess you want. I blame that on not just moderate stupidity but also the mathematics authors and teachers who by example taught me to write things like

      Suppose x = 1.

      Now they never told me to write

      Suppose 1 = x

      and why not -- because equalities are equalities and one is just as true as the other?

      It's I think because = often (usually) means "Is" (*) and has asymmetric undertones, so there is a strong sense in which

      x = 1

      is intended to convey something like "our general thing is called x, and in this particular case its value is 1" and we don't write

      1 = a

      with reversed wording because that is not the way we think.

      The tio can be found also at https://journals.sagepub.com/doi/pdf...867X0900900107 and https://journals.sagepub.com/doi/pdf...867X1101100308 but not in https://journals.sagepub.com/doi/pdf...867X0600600413

      (*) This isn't original. Somewhere Donald Knuth quotes Nicolaas Govert de Bruijn to this effect.

      Comment

      Working...
      X