Announcement

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

  • Read list of numbers from txt to use in a macro

    I have a txt file containing some numbers, say 1 5 6 17 18. I would like to import this to Stata and use those numbers as the steps in a four loop. Manually I can copy and paste the values

    Code:
    use data, clear
    
    foreach i in 1 5 6 17 18 {
       % do something with the i's
    }

    Essentially I would like to programmatically do the same thing. Here is how I believe the ingredients in Stata should look like, but I have failed to properly finalize it:

    Code:
    import delim using "text.txt", delim(" ")
    local `steps' transform data to local variable
    
    use data, clear
    
    foreach i in `steps' {
       % do something with the i's
    }
    The bold italic part is what I struggle with. Any ideas?


  • #2

    try using -levelsof- with the option to store results in the macro:
    Code:
    import delim using "text.txt", delim(" ")
    levelsof stepsvar, loc(steps)
    
     use data, clear  
    foreach i in `steps' {  
      % do something with the i's
    }
    
    
    **auto example:
    sysuse auto2, clear
    levelsof mpg, loc(steps)
    sysuse auto, clear
    di `"`steps'"'
    foreach i in `steps' {
       di `"`i'"'
       list make if mpg == `i'
    }
    Last edited by eric_a_booth; 23 Sep 2019, 06:52. Reason: Formatting update
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      This works perfectly, thanks a lot!

      Comment

      Working...
      X