Announcement

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

  • How to assign numerical values (with repeated values) of a variable into a numlist?

    Hi:

    Is there a way to assign numerical values (with repeated values) of a variable into a numlist with codes instead of manually do it?

    More details on the question:
    • The numerical values and the numlist, which will be a input for later steps, are intermediary steps in a loop.
    • I could get the length of the numerical values each time if needed, but haven’t figured out how to convert the list of numbers (with repeated values) into a numlist.
    • I tried converting the numerical values into a matrix (with one row) and then assigned to a numlist, but it did not work.
    Thanks,

    Qing
    Last edited by Qing Yan; 15 Jun 2023, 11:53.

  • #2
    I'm not sure I understand your question. You described a few things you have tried, but as you don't show the code you tried to use, it is hard to imagine what you actually did.

    So I'm going to ignore the text of your post and just respond to the title "how to assign numerical values with repeated values of a variable into a numlist." That is, I'm assuming that you have some numeric variable with various values, some of which are repeated, and you need to make a numlist from those.
    Code:
    levelsof name_of_the_variable, local(list_of_levels)
    will store those values in the local macro levels. This local macro will work as a numlist. For example, if you need to do a loop over a numlist with those values.:
    Code:
    foreach n of numlist `list_of_levels' {
    If this is not what you are looking for, please post back with 1) a clearer explanation, 2) the code that you have attempted to use so far and the output that you got from Stata as a result, and 3) example data from your data set.

    Comment


    • #3
      Hi Clyde:

      Thank you for your reply.

      My question is how to save the values of a variable into a numlist using codes. The variable has repeated values and the number of observations of the variable varies in each iteration in a loop.

      I also read a previous post suggesting using levelsof but it only saved the unique values.

      Here is what I've got using levelsof:

      . use input_data, clear

      the data:
      Click image for larger version

Name:	Screenshot 2023-06-15 144145.png
Views:	1
Size:	11.2 KB
ID:	1717307

      .
      . levelsof n, local(a_list)
      .1500000059604645 .25

      . display "`r(a_list)'"



      Here is what I have tried converting into a matrix then saved to a numlist:

      *Convert the variable as a matrix
      . mkmat n

      .
      . *transpose
      . matrix a_row = n'

      .
      . matrix list a_row

      a_row[1,13]
      r1 r2 r3 r4 r5 r6 r7 r8
      n .15000001 .15000001 .15000001 .15000001 .15000001 .15000001 .15000001 .15000001

      r9 r10 r11 r12 r13
      n .15000001 .15000001 .15000001 .15000001 .25

      .
      . numlist "`r(a_row)'"
      invalid numlist has too few elements


      Qing



      Attached Files

      Comment


      • #4
        Oh, so you actually want to repeat the repeated values in your numlist.
        [code]
        local my_numlist
        forvalues i = 1/`=_N' {
        local my_numlist `my_numlist' `=n[`i']'
        }
        display `my_numlist'

        In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 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.

        Comment


        • #5
          Hi Clyde:

          This works! Thank you so much for your help! I just tried -dataex-, and I'll use the -dataex- command to show data in the future.

          Best,

          Qing

          Originally posted by Clyde Schechter View Post
          Oh, so you actually want to repeat the repeated values in your numlist.
          [code]
          local my_numlist
          forvalues i = 1/`=_N' {
          local my_numlist `my_numlist' `=n[`i']'
          }
          display `my_numlist'

          In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 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.


          Comment


          • #6
            If you want a (Stata) matrix, you do not need to go through a local

            Code:
            mata : st_matrix("a_row", st_data(., "varname")')

            Comment

            Working...
            X