Announcement

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

  • Extract Part of String

    I have the following string variable:

    input str42 string
    "10 20 30"
    " 90 100 110"
    "150 160 170 180"
    end

    I would like to get the values of each row into a local list or at least into new variables that I can then use to create the local list. I tried levelsof but it just removes the spaces.

  • #2
    Originally posted by Trevor Andrew View Post
    I would like to get the values of each row into a local list . . I tried levelsof but it just removes the spaces.
    Are you looking for something like this?
    Code:
    forvalues i = 1/`=_N' {
        local string`i' = string[`i']
    }
    di "`string1'"
    di "`string3'"

    Comment


    • #3
      It is extremely unclear what result you are trying to get. Here are a few possibilities, perhaps one of which is what you are looking for:

      Code:
      . levelsof string, local(list)
      `" 90 100 110"' `"10 20 30"' `"150 160 170 180"'
      
      .
      . macro list _list
      _list:          `" 90 100 110"' `"10 20 30"' `"150 160 170 180"'
      Code:
      . split string, gen(items)
      variables created as string:
      items1  items2  items3  items4
      
      .
      . list, clean
      
                      string   items1   items2   items3   items4  
        1.          10 20 30       10       20       30          
        2.        90 100 110       90      100      110          
        3.   150 160 170 180      150      160      170      180
      Code:
      . forvalues i = 1/3 {
        2.         local list`i' = string[`i']
        3. }
      
      .
      . macro list _list1 _list2 _list3
      _list1:         10 20 30
      _list2:          90 100 110
      _list3:         150 160 170 180
      If none of these is what you want, when posting back please show what the desired results would be for the example data.

      Added: Crossed with #2.

      Comment


      • #4
        If Joseph Coveney's interpretation is correct (equal to Clyde Schechter's last guess). and that interpretation does to me seem closest to the wording in #1, I can't see that the result offers any advantages over the original variable.

        Otherwise put, why do you want this local macro at all?

        Comment


        • #5
          Joseph Coveney solution worked. Nick Cox I want to loop through the values of the string in each row.

          Comment


          • #6
            I smell an XY problem here. Could you explain the larger problem you are trying to crack? Why are you trying to loop through the values of these strings? The scenarios where you need to loop through the values of individual observations in Stata are, while not entirely absent, rare enough to suggest that your larger problem may have more efficient solutions.

            Comment


            • #7
              I agree with Hemanshu Kumar. I get no sense whatsoever of why and how you want to loop here, nor why you think that would be easier with local macros.

              This next comment is not a criticism, but a sociological guess: I often see people wanting to loop over observations where it is not needed in Stata, and wonder whether it's because they have previous (or more) experience in languages where that is what you would do. Stata is very supportive of loops. but also very supportive of working on variables as a whole.

              All speculation! While your question implies limited experience in Stata programming, we need concrete detail to give better advice.

              Comment


              • #8
                I am still naively or optimistically hoping for a good story here to make the problem intelligible. People who answer deserve attention too!

                Comment


                • #9
                  Nick Cox I have a data set with the mentioned string variable that has different values. Each row represents a country and each value of the string variable represents a cutoff of a treatment variable. So one country has multiple cutoffs. I then need to run a separate regression for each country using this treatment variable. To run this regression I need an indicator if the treatment variable is around the cutoffs. To create this variable I loop through the values of the cutoffs for each country.

                  Comment


                  • #10
                    Thanks for the extra detail. Sounds like split on the string variable, and putting together the results of levelsof on each result to get a combined list of levels. That's as far as I might understand the problem. No obvious need to loop over observations or create lots of local macros firtst.

                    Comment

                    Working...
                    X