Announcement

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

  • Forvalues loop not recognizing variable name

    Hi all,

    I'm sure this question has been answered before but I can't find it in search. I'm cleaning a series of dates that are numbered consecutively (int1datesurg, int2datesurg, int3datesurg). These should be always be consecutive (i.e. date2 should always come after date1). I want to create a loop that returns the ID of the observation and the first date if this is not true.

    Code:
    unab varlist : int*
    . forvalues i=1/25{
      2.         list id int`i'datesurg if int`i'datesurg > int[`i'+1]datesurg
      3.         }
    int not found
    r(111);
    I think this is happening because I have over 100 variables that start with "int#"? I know Stata can't begin variable names with numbers and I want to keep the variables named in this convention for data management . Is there a way around this? Do I need to embed a foreach loop to get Stata to recognize "int`i'datesurg" as a variable name?


  • #2
    Consider what happens at the start of your loop when i = 1. Then the list statement becomes


    Code:
    list id int1datesurg if int1datesurg > int[1+1]datesurg
    Stata parses your statement from left to right into tokens. (Here tokens are just the syntax elements that are produced by parsing.) It identifies int as a token and can make no sense of it in that context (you have no such variable or scalar) but the real problem is the incorrect guess that Stata will evaluate [`i' + 1] as 2. That's only true for subscripts, but subscript notation isn't allowed within variable names.

    The problem underlying the problem (underlying the problem) is that you're holding panel data in wide layout when long would work better. So, long-term I would recommend reshape long here. But the immediate solution is that Stata does support what you want through
    Code:
    forvalues i=1/25 {    
        list id int`i'datesurg if int`i'datesurg > int`=`i'+1'datesurg
    }
    The unab step here is irrelevant as you never use the varlist result. But your code will fail if any of int1datesurg to int26datesurg doesn't exist.

    EDIT: I would do something more like this given your data layout.


    Code:
    gen problem = 0 
    
    quietly forval j = 1/25 { 
        replace problem = 1 if int`i'datesurg > int`=`i'+1'datesurg 
    }
    
    edit id int*datesurg if problem


    Last edited by Nick Cox; 19 Jun 2018, 07:00.

    Comment


    • #3
      Oh great thanks Nick! That worked. I'm new to Stata and still figuring out the syntax. To your point on reshaping to long, I think you are right, but I wanted to make sure it will work with all the analyses first and hadn't gotten that far. It might make sense to make that decision earlier next time.

      Comment


      • #4
        In #2 the last loop should be in terms of i not j.

        Comment


        • #5
          Right I saw that. Thanks!

          Comment

          Working...
          X