Announcement

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

  • Constructing the equivalent of an “if then do” statement in Stata

    Hello Stata Community,

    I have a Stata coding question. I have a dataset where subjects received 42 lifetime victimization items, but some variables (child maltreatment, sexual victimization) were different if the subject was <18 y/o vs. >= 18 y/o. In SAS, I would use an "If then do” statement like the one listed below. I’m not sure how to construct this count variable in Stata given that it uses different variable names for those over and under 18 years old. Any help would be greatly appreciated!!!


    ****POLYVICTIMIZATION SCALES******************************************** ********************************************;

    ****LIFETIME POLYVICTIMIZATION (AGE < 18);
    POLYVIC_PREVALENCE = 0;

    IF AGE_CALC < 18 THEN DO;

    IF JVQ_CC01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC07 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_01 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_02 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_03 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_04 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_05 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_06 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_07 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV07_UNDER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI07 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE04 > 0 THEN POLYVIC_PREVALENCE + 1;

    END;

    ****LIFETIME POLYVICTIMIZATION (AGE >= 18);
    IF AGE_CALC >= 18 THEN DO;

    IF JVQ_CC01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC07 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_01 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_02 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_03 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_04 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_05 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_06 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CC08_07 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM01_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM02_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM03_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_CM04_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_PS06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV01_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV02_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV03_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_SV07_OVER18 = 1 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI04 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI05 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI06 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF JVQ_WI07 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE01 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE02 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE03 > 0 THEN POLYVIC_PREVALENCE + 1;
    IF VICTYPE04 > 0 THEN POLYVIC_PREVALENCE + 1;

    END;

    LABEL POLYVIC_PREVALENCE = 'POLYVICTIMIZATION PREVALENCE SCREENER SUM (LIFETIME) MAX RANGE: 0-42';




  • #2
    Hello Paul,

    Welcome to the Stata Forum.

    I'm not quite sure whether I understood correctly your query, if not, sorry for that.

    In spite of not being acquainted with SAS, it seems to me the commands are the same in #1, no matter the age group.

    That said, as shown here (http://www.stata.com/manuals13/gsw11.pdf), the command - replace - will probably do the trick for you.

    For example, you could type something like:

    Code:
    . replace POLYVIC_PREVALENCE = POLYVIC_PREVALENCE + 1 if AGE_CALC >= 18
    
    Or:
    
    . replace POLYVIC_PREVALENCE = POLYVIC_PREVALENCE +1  if JVQ_SV07_OVER18 ==1
    Personally, I believe we'd stay much more on the safe side if we prefer to first create - "generate" in Stata - a new variable - and only then change - "replace" - the values.

    Hopefully that helps.

    Best,

    Marcos
    Best regards,

    Marcos

    Comment


    • #3
      Well, I haven't used SAS in decades, but the code looks fairly transparent. In words, it would be this: define a positive response to any item as a value > 0, except for items JVQ_CCO8_* JVQ_*_UNDER18, and JVQ_*_OVER18, in which case only a value of 1 counts as a positive response. Final assumption: if the respondent was over 18, then the items that are just for under 18 have either missing values or 0, and vice versa. If that's a correct paraphrase, and if there are no other variables matching JVQ* or VICTYPE* than the ones you list, then the following code will do it:

      Code:
      ds JVQ_CCO8_* JVQ_*_UNDER18 JVQ_*_OVER18, local(must_be_1)
      ds JVQ_* VICTYPE*, local(all_items)
      local any_pos: list all_items - must_be_1
      
      foreach v of varlist `must_be_1' {
          gen pos_`v' = (`v' == 1)
      }
      foreach v of varlist `any_pos' {
          gen pos_`v' = (`v' > 0) & !missing(`v')
      }
      egen POLYVIC_PREVALENCE = rowtotal(pos_*)
      
      drop pos_* // OPTIONAL IF YOU DON'T NEED THIS FOR ANYTHING ELSE
      That's why I gave up SAS for Stata 22 years ago and have never looked back!

      If there are other JVQ* or VICTYPE* variables, then the second -ds- command has to be spelled out in greater detail. But even so, it isn't nearly as burdensome the SAS code writing out one variable at a time.
      Last edited by Clyde Schechter; 26 Mar 2016, 16:44.

      Comment


      • #4
        The closest equivalent to an if then do/if then else type statement is the cond() function. One of the nice things about the function is that you can nest conditional functions to prevent any overlapping logic from being implemented multiple times (e.g., the first instance that satisfies the "if" part of the statement will be implemented even if there are subsequent nested statements that an observation may also satisfy). You can also collapse some of these individual statements using the -inlist- function:

        Code:
         
        replace polyvic_prevalence = polyvic_prevelance + 1 if inlist(1, jvq_cc08_01, jvq_cc08_02, jvq_cc08_03, jvq_cc08_04, jvq_cc08_05, jvq_cc08_06, jvq_cc08_07)
        The same logic can also be nested inside of the cond() function.

        Comment

        Working...
        X