Announcement

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

  • define a matrix with local variable


    Good evening,

    i want to draw a graph bart. First i need to define a matrix but,

    When I execute these codes, stata marks "invalid syntax"

    start code******

    local xvars mb meg rta ( all categorical variable)

    local n: word count `xvars'

    matrix T = J(`n', 1, .)

    matrix colnames T = type_nbr
    matrix rownames T = `xvars'

    end code****

    I'm not familiar with matrices under stata.
    I would like to understand why this code doesn't work.

    can we use a local to define a row or column of a matrix as I tried to do.

    otherwise would anyone have any other codes to offer me?
    thank you

    l

  • #2
    can we use a local to define a row or column of a matrix as I tried to do.
    Yes it is possible and your code should work. Why did you highlight

    Code:
    matrix T = J(`n', 1, .)
    Is this command returning an error?

    Comment


    • #3
      I cannot replicate the problem you are having. The code runs without error messages on my setup.

      I do note one thing in your code which is probably incorrect, but it does not trigger an error message:
      When you write -local xvars mb meg rta ( all categorical variable)-, the text "( all categorical variable)" is part of local macro xvars. Consequently when I run your code, and then list matrix T, Stata gives me:

      Code:
      . matrix list T
      
      T[7,1]
                   type_nbr
               mb         .
              meg         .
              rta         .
                (         .
              all         .
      categorical         .
        variable)         .
      I suspect this is not what you intended. I imagine you intended only to include mb, reg, and rta in the local macro xvars. If I am right about that, you need to set off "( all categorical variable)" as a comment:
      Code:
      local xvars mb meg rta // ( all categorical variable)

      Comment


      • #4
        Originally posted by Jack Benhayer View Post

        Yes it is possible and your code should work. Why did you highlight

        Code:
        matrix T = J(`n', 1, .)
        Is this command returning an error?
        yes it's this command. thank you very much for your answer

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          I cannot replicate the problem you are having. The code runs without error messages on my setup.

          I do note one thing in your code which is probably incorrect, but it does not trigger an error message:
          When you write -local xvars mb meg rta ( all categorical variable)-, the text "( all categorical variable)" is part of local macro xvars. Consequently when I run your code, and then list matrix T, Stata gives me:

          Code:
          . matrix list T
          
          T[7,1]
          type_nbr
          mb .
          meg .
          rta .
          ( .
          all .
          categorical .
          variable) .
          I suspect this is not what you intended. I imagine you intended only to include mb, reg, and rta in the local macro xvars. If I am right about that, you need to set off "( all categorical variable)" as a comment:
          Code:
          local xvars mb meg rta // ( all categorical variable)
          thank you for your answer. Sorry i've just added this comment. It's not part of the code.
          I really don't understant the matter. i run stata 14. is it possible that the syntax is different or has changed ?

          Comment


          • #6
            This is the complete code.
            but but the error message appears at the command highlighted in red

            local xvars mb meg rta

            ***matrix for output

            local n: word count `xvars'

            matrix T = J(`n', 1, .)
            matrix colnames T = type_nbr
            matrix rownames T = `xvars'

            foreach var in `xvars' {
            global `var'label : var label `var'


            count if `var'==1
            matrix T[rownumb(T, "`var'"), colnum(T, "type_nbr")]=`r(N)'
            }

            clear
            svmat T
            rename T1 type_nbr

            gen Fintype =""
            order Fintype

            local i=1
            foreach var in `xvars' {
            replace Fintype = "$`var'label" in `i'
            local i= `i'+ 1

            graph bar type_nbr, over (Fintype)

            Comment


            • #7
              My guess is that you're executing the code line by line from a do-file editor window. If so, then the definition of local macro n is hidden from the line that first uses it. All that said, this looks like a way to draw a bar chart.

              Code:
              gen count = .
              gen which = ""
              local i = 1
              
              foreach v in mb meg rta {
                   count if `v' == 1
                   replace count = r(N) in `i'
                   replace which = "`: variable label `v''" in `i'
                   local ++i
              }
              
              graph bar (asis) count, over(which)
              is shorter code for what I think you want,

              See also https://www.stata.com/statalist/arch.../msg01258.html

              Comment


              • #8
                Thank you very much! the code works perfectly. I will avoid executing my code line by line and I will see what it looks like. thank you very much


                Originally posted by Nick Cox View Post
                My guess is that you're executing the code line by line from a do-file editor window. If so, then the definition of local macro n is hidden from the line that first uses it. All that said, this looks like a way to draw a bar chart.

                Code:
                gen count = .
                gen which = ""
                local i = 1
                
                foreach v in mb meg rta {
                count if `v' == 1
                replace count = r(N) in `i'
                replace which = "`: variable label `v''" in `i'
                local ++i
                }
                
                graph bar (asis) count, over(which)
                is shorter code for what I think you want,

                See also https://www.stata.com/statalist/arch.../msg01258.html

                Comment

                Working...
                X