Announcement

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

  • Recreating SPSS vector commands in Stata

    Dear list,

    I am currently charged with the translation of an .sps-file to Stata. In general this is quite easy but there a certain SPSS-command I just cannot reproduce:

    if (selfall2(POINTB) ge 1 & selfall2(POINTB)le 3) FJSELFE=10.

    selfall2 is a vector comprised of 15 variables and POINTB a variable with 15 attributes. For each observation the code checks the attribute of the variable and accesses the n-th element of selfall, where n is equal to the value of pointb, and examines whether this variable fullfills the if-condition or not.

    Is there any way how I can achieve the same effect in Stata?

    Thank you
    Tobias Wolfram

  • #2
    I don't understand the part of your description that states
    For each observation the code checks the attribute of the variable and accesses the n-th element of selfall, where n is equal to the value of pointb, and examines whether this variable fullfills the if-condition or not.
    To me, the issue is your statement that "the code checks the attribute of the variable" Which variable? Let selfall2_a be the first variable in the selfall2 vector of 15 variables, selfall2_b the second, and so on. What precisely is the long-hand description of the FJSELFE generation rule? Is it something like the following?.

    Code:
    ge fjselfe = .
    replace fjselfe = 10 if  (selfall2_a == ???  | selfall2_b == ??? | selfall2_c == ??? | ... )  & (pointb <= 3)
    In short, please spell out in words, in detail, what the SPSS command actually does.

    Comment


    • #3
      Thank you for your reply and please excuse my insufficient explanation. I will give my best to illustrate the case.

      selfall2 is a vector, which means it is an SPSS data structure which contains a certain amount of variables. Like an array you can address the variables inside the vector with selfall2(varname). The variable POINTB has as many possible values as variables in selfall2 exist. So if I call selfall2(POINTB) and POINTB has for an observation A the value 3 it will access for this observation A selfall2(3), meaning it will check if the third variable inside the vector satisfies the if-condition for A.The critical aspect is that another observation B could have a different value of POINTB, for example 7. For this observation B the vector is selfall2(7), so it will access the seventh variable inside the vector to check whether it satisfies the if-condition. I hope I have made my case clearer now.

      Comment


      • #4
        Short answer. There is no equivalent in Stata. Blocks of variables in Stata are just blocks of variables. You could get closer to this in Mata, although my guess is that you just need to rethink how to do it in Stata.

        If your task is to translate SPSS code into Stata, the presumption that every language construct in SPSS has a direct equivalent in Stata is likely to be wrong, just as is the converse.

        Although it's not aimed at your question. http://www.stata.com/support/faqs/da...nt-sas-arrays/ may be helpful.

        Comment


        • #5
          I believe that Steven has pointed in the direction you will have to go. Given your additional information, I think what you're looking at is a series of 15 replace statements, something like the following, assuming that pointb takes the values 1, 2, 3, ..., 15 and the corresponding variables in the selfall2 list are named selfall2_a ... selfall2_o.
          Code:
          generate fjselfe = .
          replace flselfe = ?? if pointb==1 & selfall2_a>=1 & selfall2_a<=3
          replace flselfe = ?? if pointb==2 & selfall2_b>=1 & selfall2_b<=3
          replace fjselfe = 10 if pointb==3 & selfall2_c>=1 & selfall2_c<=3
           . . .
          replace flselfe = ?? if pointb==15 & selfall2_o>=1 & selfall2_o<=3

          Comment


          • #6
            As Nick says, there is no direct equivalent in Stata. It may be desirable to re-write the code from first principles, but in the event that a "literal" translation is needed, something like the following will echo what SPSS does.

            First, unab creates a list of the 15 variables (they need to be consecutive), and then the loop picks out each element of the list, and sets the value if the variable is in the range and if pointb has the value of the loop index.

            Code:
            unab variablelist : selfallA-selfallO
            gen fjselfe = .
            forvalues i = 1/15 {
            local selfall : word `i' of `variablelist'
            replace fjselfe = 10 if inrange(`selfall',1,3) & pointb==`i'
            }

            Comment

            Working...
            X