Announcement

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

  • create a new matrix each time through a loop

    Hello,
    My first time using a matrix (and posting a question here). Please help if you can.
    I am trying to make a loop which will create a new matrix of correlations each time through a loop. I need something like this:
    forvalues statamonth = 372(1)707 {
    pwcorr Australia Austria Belgium Canada if statamonth==‘i’
    create a matrix = r(C)
    }
    So for each month, I need a correlation matrix between the values for the different countries.
    Thank you very much,
    Stan

  • #2
    It's just a small modification of your pseudocode:
    Code:
    forvalues i = 372(1)707 {
        pwcorr Australia Austria Belgium Canada if statamonth==‘i’
        matrix C`i' = r(C)
    }
    That will create matrices named C372, C373,...,C707.

    That said, what are you going to do with these 336 matrices? It is generally not useful to put things into a Stata matrix unless you plan to do some actual matrix algebra with them. Depending on where you're going with this, there are probably better ways to collect these results.

    Finally, I'm going to make, with high confidence, a guess that Stan Researcher is not your real name. It is the norm in this community that we use our real given and family names as our user name, to promote collegiality and professionalism. It is not possible for you to change your username by editing your profile, but you can click on CONTACT US in the lower right hand corner of this page and message the System Administrator requesting that it be done for you. Your adherence to this norm is appreciated.



    Comment


    • #3
      Thank you, Clyde.
      I do need to do some matrix algebra with the resulting matrices.
      I will definitely consider "changing" my name, but for now I'm new here and I just want to make sure I'm comfortable.

      Comment


      • #4
        Note that Clyde overlooked a typo in your original code. The command
        Code:
        pwcorr Australia Austria Belgium Canada if statamonth==‘i’
        should be
        Code:
        pwcorr Australia Austria Belgium Canada if statamonth==`i'
        Where "smart quotation marks" in Microsoft Word and similar programs yield something that looks like
        Code:
         ‘shortcut’
        you need to type on your keyboard
        Code:
         `shortcut'
        where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. Put another way, don't type your code in an editor with "smart quotes" and then copy it into Stata.

        Comment


        • #5
          Let me add a piece of Statalist - rather than Stata - advice.

          To become more comfortable with Statalist, and to ensure other members are comfortably able to address your questions, please take the a few moments to a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The omission of code delimiters made your typographical error easy to overlook.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Comment


          • #6
            Thank you, William Lisowski, for picking up that oversight of mine. And, yes, you are right that had that code been posted between code delimiters I would have been more likely to notice it.

            Comment


            • #7
              Hello, just to follow up, I'm getting an invalid syntax error in the execution of the loop when I do the following:
              Code:
              reshape wide indexcode CHG_PCT_1D CUR_MKT_CAP, i(date) j(country) string
              gen statamonth = mofd(date)
              forvalues statamonth = 480(1)707 {
                  pwcorr CHG_PCT_1DAustralia CHG_PCT_1DAustria CHG_PCT_1DBelgium CHG_PCT_1DCanada CHG_PCT_1DDenmark CHG_PCT_1DFinland CHG_PCT_1DFrance CHG_PCT_1DGermany CHG_PCT_1DGreece CHG_PCT_1DHongKong CHG_PCT_1DIreland CHG_PCT_1DItaly CHG_PCT_1DJapan CHG_PCT_1DNetherlands CHG_PCT_1DNewZealand CHG_PCT_1DNorway CHG_PCT_1DPortugal CHG_PCT_1DSingapore CHG_PCT_1DSpain CHG_PCT_1DSweden CHG_PCT_1DSwitzerland CHG_PCT_1DUnitedKingdom CHG_PCT_1DUnitedStates if statamonth==`i'
                  matrix C`i' = r(C)
              }
              Where am I going wrong?

              Thank you all!

              Comment


              • #8
                You are going wrong in both the -pwcorr- and -matrix- commands where you reference a local macro `i' that has never been defined. The loop is being iterated over a local macro with a different name: statamonth, not i. So either change `i' to `statamonth' in both of those commands, or, perhaps more simply, change statamonth to i (no quotes around it) in the -forvalues- command.

                Comment


                • #9
                  Ah, yes, thank you very much!
                  One more question, if I may: is there a way to take a matrix and have the data in memory be replaced by that matrix (so that one can easily copy and paste to excel)? Something similar to how collapse replaces the data in memory with something very different.
                  Thank you for your help.

                  Comment


                  • #10
                    You can bring a matrix into the data with the -svmat- command. See -help svmat-.

                    But unless you are just playing around, you should not be transferring data by copy and paste. It is very error prone, it leaves no audit trail, and it should never be used for serious work. Stata has an -export excel- command to transfer data into Excel, and, perhaps even more appropriate for your situation, you can use the -putexcel- command to transfer a matrix into Excel without moving it through the data first. Familiarize yourself with these essential command by reading their help files and the corresponding sections in the PDF manuals that come with your installation.

                    Comment


                    • #11
                      Thank you very much!

                      Comment

                      Working...
                      X