Announcement

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

  • Update counting local macro conditionally

    Hello everyone! I need to use a for-loop to iterate through about 300 excel sheets that follow a pattern where every 3 sheets covers a year, and the three sheets are for male, female, and total data. I need to use 2 local macros as count variables to keep track of the two identifying features of each excel sheet. However, I have run into issues, and after testing basic output in the command window, that you can see below, I am failing to understand Stata's logic:

    . local x = 1

    . display `x'
    1

    . local y = 5

    . display `y'
    5

    . if `y' == 5 { local x = `x' + 1 }
    . display `x'
    1

    I want Stata to be increasing local macro x to 2 if local macro y is equal to 3 here. It runs the if command I have set up without error, but does not actually update x. Can someone tell me how to have Stata check for the value of a local macro and update another or the same local macro, or run other commands, accordingly? Thank you very much for your time and help!
    Last edited by Nate Tillern; 25 Jun 2019, 09:24.

  • #2
    It should be:

    Code:
    . local x = 1
    
    . display `x'
    1
    
    .
    . local y = 5
    
    . display  `y'
    5
    
    .
    . if `y' == 5 {
    .         local x = `x' + 1
    . }
    
    . display `x'
    2
    If you read -help ifcmd- you will see:
    If you put braces following the if or else,

    1. the open brace must appear on the same line as the if or else;

    2. nothing may follow the open brace except, of course, comments; the first command to be executed must appear on a new line;

    3. the close brace must appear on a line by itself.

    Comment

    Working...
    X