Announcement

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

  • Simple question about error in "create new variable"

    Hello STATA experts,

    I am not very STATA savvy and hence my question is probably very silly..
    I am trying to create new variables by extracting certain values within one column of values (ml of blood, in case it is important). I need to create 5 variables according to amount of blood. My intent is to use those variables for a logistic regression later.

    These are the 5 variables I am trying to create (numbers represent ml, so 3ml, 6ml, 10ml; <= 0 ml is possible because the values were calculated post-pre values and pre is occasionally larger than post)

    Var1: <= 0
    Var2: >0 & <=3
    Var3: >3 & <=6
    Var4: >6 & <=10
    Var5: >10

    I picked "create new variable", entered the variable name and then did the first expression 'PHEexpansion <=0' which worked perfectly.
    As soon as I used the logical operator "&", it gave me an error saying "<=3 invalid name". The expression I entered into GUI was 'PHEexpansion >0 & <=3'

    I tried to enter it with and without blanks which both gave me the same error. I also tried to enter the command directly into the command window without a change in result.

    Does anyone have a suggestion what I am doing wrong here? I would be very grateful if I could make this work.

    Thank you so much!
    best,
    Sarah

  • #2
    Think of the logical operators as combining (complete) expressions. The expression <= 3 is by itself incomplete. Stata asks: what should be lower or equal to three? You must repeat the variable name on both sides of the & operator to complete the expression.

    Comment


    • #3
      Hello All,

      Please could you help me?

      Could you help me identify what's not working in the code below, please? What I'm trying to do is find the second value after the minimum in a set of 7 variables, while also considering conditions regarding other variables. When executing this code, it often happens that the two variables created have the same values, and the condition that the second variable should be greater than the first is not met.

      Here is the code:

      gen Var1 =.y
      forvalues i=1/7 {
      replace Var1 = ACHB`i'C if ACHB`i'C < Var1 & ~missing(ACHB`i'C) & RCI30_`i'== 1
      }

      gen Var2 =.y
      forvalues i=1/7 {
      replace Var2 = ACHB`i'C if (ACHB`i'C > Var1 & ACHB`i'C < Var2) & ~missing(ACHB`i'C) & (RCI30_`i'== 1)
      }

      Thank you for your assistance.

      Comment


      • #4
        Triplicate post!!! If interested, follow at https://www.statalist.org/forums/for...ns-within-loop.

        Please stop re-posting the same question in multiple places. And be patient! This is not a help line; it is a community of users who are here by choice. Your question will be answered (has been answered, in this case) if and when somebody who sees it chooses to respond to it. In general, you should expect hours to pass, longer on weekends or holidays. If that timeline is not suitable to your needs, you should seek advice elsewhere.

        And, in any case, if you do not receive an answer within, say, 24, hours, when you re-post you should attempt to improve your question. Most unanswered questions are passed over because they are confusingly written, or lack sufficient information to provide an answer. Alternatively, they may be in niche areas where no Forum participants have the requisite knowledge. Please read the Forum FAQ for excellent advice on how to ask questions in a way that maximizes your chance of drawing a timely and helpful response.

        Comment


        • #5
          daniel klein I figured it would be something silly, simple, but I wouldn't have come up with it. Thank you so much for that great explanation and information. It worked like a charm. :-)

          Comment

          Working...
          X