Announcement

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

  • Reordering the elements of a local based on a variable

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str15 fruit float year
    "orange" 2017
    "banana" 2015
    "apple"  2000
    end
    Suppose i have the local fruit that contains, in this order, the lemeents "banana" "apple" "orange". How can I order the elements of this local based on a reverse chronological order, i.e. based on the descending valuees of the year variable? i.e. local fruit = "orange" "banana" "apple" since 2017 > 2015 > 2000.

    PS : I did not use levelsof to get the local fruit. Assume the local was manually defined and that it will always be a subpart of a much larger dataset.

    Thank you all for your help !!

  • #2
    I’m not quite clear on your problem setup. Are you creating the local from your dataset, in which case you can manipulate the order of the data prior to creating the local macro? Or do you have some pre-existing macro that you want to sort post hoc? The first option is easier to work with.

    Comment


    • #3
      Leonardo : Sorry for the unclearness. Indeed I am creating this local from my dataset so the first option is correct, however there is no automatized way to create my local.

      It's like my full dataset would be composed of :

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str22 word float year
      "banana" 2017
      "cat"    1998
      "apple"  1978
      "truck"  2005
      "pear"   2004
      "orange" 2010
      end
      it's like I want to create a local composed only of fruits (this I must do it manually by just doing local fruit banana apple pear), but sorted by descending year. Why don't I just add manually the sorted fruits? Because I would like to avoid as much as possible the amount of input by hand and I could overlook some stuff. Since I also work with people, I also wish anyone to do a thematic local (in this dumb example, animals, machines etc) to just gather the words without worrying about the year as the code would sort it automatically.

      By the way, one fruit is always equal to one unique year so no need to worry about duplicates.

      Comment


      • #4
        Get a list of fruits into a dataset. Then, merge that dataset with your original data:

        Code:
        use mydata
        merge 1:1 word using list_of_fruits.dta , keep(master matched)
        Now, _merge==3 marks the fruits. Next, look through the list of unmatched words:

        Code:
        edit word _merge if _merge == 1
        and manually change _merge to 3 for all fruits that have not been matched.

        keep the fruits and sort by year:

        Code:
        keep if _merge==3
        sort year
        Then create your local, e.g.,

        Code:
        mata : st_local("fruits", invtokens(st_sdata(.,"word")'))

        Optionally, skip the first steps, go through all your data and mark the fruits in a new variable:

        Code:
        generate byte is_a_fruit = 0
        edit word is_a_fruit


        Edit:

        Proof of concept:

        Code:
        . list
        
             +--------------------------+
             |   word   year   is_a_f~t |
             |--------------------------|
          1. | banana   2017          1 |
          2. |    cat   1998          0 |
          3. |  apple   1978          1 |
          4. |  truck   2005          0 |
          5. |   pear   2004          1 |
             |--------------------------|
          6. | orange   2010          0 |
             +--------------------------+
        
        .
        . sort year
        
        .
        . mata : st_local("fruits", invtokens(st_sdata(.,"word","is_a_fruit")'))
        
        .
        . display "`fruits'"
        apple pear banana

        Edit 2: Just realized that you want descending order. Use gsort or multiply year by -1 before sorting.
        Last edited by daniel klein; 18 Sep 2023, 07:39.

        Comment

        Working...
        X