Announcement

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

  • Loop with the command usespss in Stata 16

    Hi. I have to do the same calculations with some data sets in format .sav. I did this lines but they do not work.

    set more off

    global archivos_enemdu "ENEMDU_PERSONAS_2007_12_hom.sav ENEMDU_PERSONAS_2008_12_hom.sav ENEMDU_PERSONAS_2009_12_hom.sav ENEMDU_PERSONAS_2010_12_hom.sav ENEMDU_PERSONAS_2011_12_hom.sav ENEMDU_PERSONAS_2012_12_hom.sav ENEMDU_PERSONAS_2013_12_hom.sav ENEMDU_PERSONAS_2014_12_hom.sav ENEMDU_PERSONAS_2015_12_hom.sav ENEMDU_PERSONAS_2016_12_hom.sav ENEMDU_PERSONAS_2017_12_hom.sav ENEMDU_PERSONAS_2018_12_hom.sav ENEMDU_PERSONAS_2019_12_hom.sav ENEMDU_PERSONAS_2020_12_hom.sav ENEMDU_PERSONAS_2021_12_hom.sav"

    foreach a of global archivos_enemdu{
    usespss "ENEMDU_PERSONAS_`a'_12_hom.sav"
    gen edad=.
    replace edad=1 if p03>=0 & p03<=5
    replace edad=2 if p03>=6 & p03<=11
    replace edad=3 if p03>=12 & p03<=17
    replace edad=4 if p03>=18 & p03<=29
    replace edad=5 if p03>=30 & p03<=64
    replace edad=6 if p03>=65
    label define label_edad 1"Infantes" 2"Niños/as" 3"Adolescentes" 4"Jóvenes adultos" 5"Adultos" 6"Tercera Edad"
    label values edad label_edad
    }

  • #2
    Next time you post, please read the Forum FAQ beforehand. Among the things you will learn there is that you should never say something "didn't work." There are many ways in which something can go wrong. You always need to explain what actually happened. Did Stata crash? Did it give you any error messages? If so, what were they? Did Stata proceed without error messages but produce incorrect results? If so, what were the incorrect results, and what do you think they should have been? Always show not just the code you ran, but show exactly how Stata responded.

    That said, in this case, it is pretty clear what the problem is. The iterator -a- in your -foreach- loop takes on the values given in the global macro archivos_enemdu. So, for example, the first time through the loop, `a' == "ENEMDU_PERSONAS_2007_12_hom.sav". So Stata expands the -usespss- command as:
    Code:
    usespss "ENEMDU_PERSONAS_ENEMDU_PERSONAS_2007_12_nom.sav_12_hom.sav"
    And there is no such file, of course.

    The simplest solution here is to have the loop iterate just over the numbers between 2007 and 2021 and leave the rest of the code as is:
    Code:
    forvalues a = 2007/2021 {
        usespss"ENEMDU_PERSONS_`a'_12_hom.sav", clear
        // etc.
    }
    Notice that I have also added the -clear- option to the -usespss- command. This is necessary because, without it, on the second pass through the loop, when `a' == 2008, Stata will halt with an error message because there is data (from the 2007 file) still in memory, and Stata will not load a new file unless you explicitly say it can. That's what the -clear- option does.

    If the only purpose of that global macro archivos_enemdu is for use in the loop you wrote, then just get rid of it as it serves no real purpose. If you need it for something else in the code, well that's different. It would be better to use a local macro instead of a global, but I will spare you my rant against global macros at this time.

    Comment

    Working...
    X