Announcement

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

  • Basic question on forval loops

    For years I've used workarounds to handle the following type of problem. I've decided finally to appeal to the collective wisdom of the StataList community.

    Suppose I have variables named y01, ..., y99. I wish to manipulate each in the same manner, possibly in a forval loop.
    Code:
    forval j=01/99 {
    sum y`j'
    }
    does not work, as might be expected, since y01-y09 are treated as if they are named y1-y9 in the sum command.

    Might anyone suggest an elegant way to handle this? The solution would ideally generalize to y001-y999, etc.

    Note: Ideally I would like to avoid using
    Code:
    foreach z of varlist y01-y99 {
    since variables other than y01-y99 might get caught in the y01-y99 varlist. But if this is the best way to proceed then so be it and I presume that something like
    Code:
    preserve
    order ..., alpha
    foreach ... {
    ...
    }
    restore
    would suffice.

    Thanks in advance.

  • #2
    Does it help to use the -strofreal()- function directly in the executed command?

    Code:
    clear
    set obs 20
    set seed 02112025
    
    forval j=1/100 {
        gen year`=strofreal(`j', "%03.0f")'= rnormal()
    }
    
    forval j=1/100 {
        sum year`=strofreal(`j', "%03.0f")'
    }
    Res.:

    Code:
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year001 |         20   -.3131233    .8361902  -2.262398   1.600142
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year002 |         20   -.2446142    1.054504   -2.07622   1.497459
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year003 |         20    .1669631    .8996776  -1.563128   1.627598
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year004 |         20   -.0029187    1.253589   -3.32213   1.362264
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year005 |         20    .1705304    1.187082  -2.977926   2.038345
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year006 |         20   -.0060827    .9878331  -1.379215   1.897073
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year007 |         20    .1206786    1.028119   -2.03441   1.801567
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year008 |         20   -.0324168     1.09227  -1.571728   2.344796
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year009 |         20    .2542958     .947083  -1.165808   2.191764
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year010 |         20    -.226149    1.065827  -2.004956    1.75305
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year011 |         20    .0607576    1.150555  -2.167209   2.340534
    
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year090 |         20   -.0961276    1.141413  -2.044078   2.192554
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year091 |         20    .4514179    .8503474  -1.116466   2.571808
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year092 |         20   -.0775707    1.032296  -1.651642   2.041872
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year093 |         20   -.0283833    .8702842  -1.428963   1.786483
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year094 |         20    .4301899    1.056002  -1.230704   2.306427
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year095 |         20    .4168104    .7539987  -.7983195   1.438262
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year096 |         20    .2585093    1.148106  -1.881129   2.475704
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year097 |         20    .1610862     .719974   -1.65291   1.351457
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year098 |         20   -.1067993    .6091672  -1.522634   .7087622
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year099 |         20   -.0541567    1.058966  -1.870191   2.065479
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         year100 |         20    .3281591    1.037528  -1.446076   2.611331

    Comment


    • #3
      Thanks Andrew. This is a nice solution.

      Comment


      • #4
        How about
        Code:
        foreach z of varlist y?? {

        Comment


        • #5
          Thanks Jeff. That will certainly do the trick.

          Comment


          • #6
            See also earlier discussions in

            SJ-21-4 pr0075 . . . . . . . . . . . . . . Speaking Stata: Loops in parallel
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q4/21 SJ 21(4):1047--1064 (no commands)
            tutorial discussing looping in parallel using foreach
            and forvalues

            https://journals.sagepub.com/doi/pdf...6867X211063415

            (Section 2 discusses again how to insert leading zeros)

            SJ-21-2 pr0074_1 . . . . . . Erratum: Speaking Stata: Loops, again and again
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q2/21 SJ 21(2):555 (no commands)
            erratum for a line of code

            https://journals.sagepub.com/doi/pdf...6867X211025839

            SJ-20-4 pr0074 . . . . . . . . . . . . Speaking Stata: Loops, again and again
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q4/20 SJ 20(4):999--1015 (no commands)
            provides updated guidance on using foreach and forvalues for
            looping through lists of values and repeating commands using
            members of those lists in turn

            (basic tutorial)
            https://journals.sagepub.com/doi/pdf...36867X20976340

            SJ-10-1 pr0051 . . . . . . . . . . . . Stata tip 85: Looping over nonintegers
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
            Q1/10 SJ 10(1):160--163 (no commands)
            tip on using forvalues to loop over numbers

            https://journals.sagepub.com/doi/pdf...867X1001000115

            (a solution similar to that of Andrew Musau)

            Comment


            • #7
              I also often find myself needing to loop over variables that have a particular pattern in their names, but -ds- and varlist wildcards such as "??" don't always cut it. So I turn to regex:

              Code:
              clear all
              
              gen y01 = .
              gen y02 = .
              gen y03 = .
              gen y04 = .
              gen yy05 = .
              gen x05y = .
              gen y04a = .
              
              foreach var of varlist _all {
                  if regexm("`var'", "^y[0-9]+$") {
                      disp "variable `var' - matched regex"
                      sum `var'
                  }
              }



              Comment


              • #8
                Evgeny Saburov I don't quite follow.

                In your example, if I just do
                Code:
                . sum y??
                
                    Variable |        Obs        Mean    Std. dev.       Min        Max
                -------------+---------------------------------------------------------
                         y01 |          0
                         y02 |          0
                         y03 |          0
                         y04 |          0
                which are the same variables your regex matched. Could you give a better example?


                Comment


                • #9
                  Hemanshu Kumar

                  I should have used a better example of variables that would match with "??" but not with regex. For example something like "ya4" would also match with y??, perhaps unintentionally, if we were only interested in variables that start with "y" and end with an arbitrary number of digits. My point is that it's just easier and safer to define an accurate pattern of variable names that are of interest with regex.
                  Last edited by Evgeny Saburov; 09 Nov 2025, 11:09.

                  Comment


                  • #10
                    Hello guys.
                    I am new to Stata. I am trying to replace "I don't know" with "0", but the replacement is not working. What am I missing here? I need your help.
                    Manually, I cannot replace all these options with "0" because I have over 1000 observations in the dataset.

                    Comment


                    • #11
                      There is very little chance you will get a timely and helpful reply to this post. You have placed it at the end of a thread that it is entirely unrelated to. So somebody who might be interested in answering a question like this one will never know it's even here. You don't show any example data, so nobody but you knows what kind of variable you are working with: it matters greatly, as different solutions would apply to different kinds of variables. And, finally, you said that "the replacement is not working." But what does that mean? There are many ways in which a command fails to work: did it crash your computer, cause Stata to close, give you an error message, none of the preceding but produced output or changes to your data that you didn't expect? And what specific command is the "replacement" you attempted?

                      So, in short, you have given almost no information about your problem and posted it in a place where the most helpful people are unlikely to see it. To get help, I recommend:

                      1. Do the post over, starting a new thread, and give that thread a title that says what the question is about.
                      2. Show example data, using the -dataex- command to do that. If you are running version 16 or later, or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
                      3. Show the exact command you tried, and show any output or messages that Stata gave in the Results window. If there was no message or output, then explain what Stata actually did. If Stata did do something to the variable, but it was not what you expected, show what the variable looked like after the command was run: probably it's easiest to do that by running -dataex- again, but perhaps -list- will do the trick.

                      More generally, I recommend you read the Forum FAQ for lots of helpful tips on how to ask questions effectively, ensuring your best chance of getting a useful reply. You can access the FAQ by clicking on the "FAQ" button that you will find just below the Statalist banner at the top of the page.

                      Comment


                      • #12
                        Thank you for helping me to better frame the question.
                        The variable is a string (str) with non-numeric characters: "Yes," "No," and "I don't know." I am assessing respondents' knowledge of hepatitis B virus curability, and the possible responses are "Yes," "No," and "I don't know."
                        The responses "No" or "I don't know" should be scored "0." I received no red error message in the output window, but those characters were not replaced with "0."

                        command in Stata 18, "replace Q2= "0" if Q2=="No" | Q2=="I don't know". All the "No" were replaced, but not the "I don't know" responses.
                        In the output window/panel, below the command lines, it says "0 real changes made," and I confirmed this by tabulating the variable containing such responses. The responses were still the same because they were not replaced with the "0s" I wanted.

                        I hope I have provided enough information to better describe the issue I am facing now. I appreciate your swift response, Mr. Schechter.
                        Last edited by Yankuba Jarjusey; 10 Nov 2025, 03:03.

                        Comment


                        • #13
                          Originally posted by Yankuba Jarjusey View Post
                          I appreciate your swift response, Mr. Schechter.
                          Try looking for responses on this thread.

                          Comment


                          • #14
                            Thanks Joseph.

                            Comment

                            Working...
                            X