Announcement

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

  • Arrays

    Hi,

    I need to generate an array in stata, but information I have read only seems to talk about arrays in mata.

    Specifically the code i need to generate is:

    Code:
    gen double Iethrisk[10] = {
    0,
    0,
    0.2804031433299542500000000,
    0.5629899414207539800000000,
    0.2959000085111651600000000,
    0.0727853798779825450000000,
    -0.1707213550885731700000000,
    -0.3937104331487497100000000,
    -0.3263249528353027200000000,
    -0.1712705688324178400000000
    }
    ...

    Can this be made sense of in stata?

  • #2
    First of all, accidently you are in the wrong fora. You should take questions on Stata to the general fora.

    That said, you can't save more than one value in a cell if that is what you are trying.
    To save few values I would use matrices.

    Code:
    . matrix Iethrisk = 0,0,0.2804031433299542500000000,0.5629899414207539800000000, ///
    >         0.2959000085111651600000000,0.0727853798779825450000000,-0.1707213550885731700000000, ///
    >         -0.3937104331487497100000000,-0.3263249528353027200000000,-0.1712705688324178400000000
    
    . matlist Iethrisk
    
                 |        c1         c2         c3         c4         c5         c6         c7         c8         c9        c10 
    -------------+--------------------------------------------------------------------------------------------------------------
              r1 |         0          0   .2804031   .5629899      .2959   .0727854  -.1707214  -.3937104   -.326325  -.1712706 
    
    . matrix Iethrisk = Iethrisk'
    . svmat Iethrisk
    The last two lines transposes the vector/matrix and save the column vector as values for a single variable
    Kind regards

    nhb

    Comment


    • #3
      Thank you Niels

      Comment


      • #4
        For those who find this post in the future, I note that the code in #1 consists of C++ code taken from the link supplied in post #5 at

        https://www.statalist.org/forums/for...nto-stata-code

        prefixed with a Stata gen command. But Iethrisk[10] means something much different in C++ than it does in Stata.

        Comment


        • #5
          William does this mean the suggested code by Niels is incorrect?
          Thanks

          Comment


          • #6
            My post #4 was intended, not for you, but for members who in the future find this topic and might wonder about the syntax in post #1. It does not mean that the suggested code in post #2 is incorrect.

            As I implied, the code you posted in #1 is neither C++ nor, as Niels Henrik Bruun assumed, poorly formed Stata, which was why he suggested you were trying to place 10 values into a single cell.

            As was pointed out byNick Cox in post #2 at

            https://www.statalist.org/forums/for...uous-variables

            you do no favors to those who want to help you by giving incomplete information about your code - or in this case, completely inventing code that misleads the reader.

            Niels took the time to figure out what you wanted and supplied a solution that correctly does what he describes. Since he likely has not been following along on your odyssey of converting C++ to Stata that started at

            https://www.statalist.org/forums/for...nto-stata-code

            he might have had different advice in the context of the entire problem, or he might have given further advice on how to incorporate his solution into the task you have undertaken.

            Comment


            • #7
              Dear all
              I did recognize that it was an attempt to translate one language into another.
              I don't think that this should be a reason not to help if possible.
              It could be a way of learning Mata/Stata - I learned Mata using Python as a reference.

              That said, Alex Williams I have some points:
              1. My code works, but it might not be best solution in a greater scheme as you clearly are working in
              2. Translating code directly is seldom a good idea
              3. Choose a programming base (eg Mata), read books and the internet regarding that base, try code examples, etc...
              Then there is a chance of you transforming your code into some workable Mata code.
              But you have to do the most work yourself - There are no free lunches at the end of the day.
              Good luck!
              I hope I can help you another day

              PS: I'm actually moved to Stata 16, but I can't figure out where to set text correct
              Last edited by Niels Henrik Bruun; 23 Nov 2019, 03:22.
              Kind regards

              nhb

              Comment


              • #8
                Niels Henrik Bruun -

                Let me say, the path to editing the signature is very well hidden. Even though I knew it was hard to find but thought I knew where it is, I missed it several times before finding it.

                To change your signature, click on your name in the upper right corner of this window, and from the drop-down menu select User Settings. The User Settings page opens with the Profile tab selected; click on the Account tab. Scroll down the Account settings to Conversation Detail Settings. Click Edit Post Signature in that area, even though the bulk of that area seems to be about how posts are displayed rather than how they are created.

                For those who find this post at a later date, Niels currently has as his signature
                Kind regards

                nhb (Moved to Stata 15.1)
                But the signature line, like the avatar, is not static content appended at the time the post was written. Instead, it is dynamically added at the time the post is displayed. So once Niels updates his signature, it will then display something different, and the meaning his PS in post #7 will no longer be apparent.

                That also explains why one can look at old posts and see that some members appear to be running a version of Stata that did not exist at the time of the post containing the signature. Either that, or time travel.

                Comment


                • #9
                  William: This information (both how to make the changes as well as the time-travel warning) is valuable. I wonder if it might be worth posting on the main StataList forum with a descriptive topic so that it can be found easily.

                  Comment


                  • #10
                    William Lisowski Thank you very much
                    Kind regards

                    nhb

                    Comment

                    Working...
                    X