Announcement

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

  • Creating a rank numeric variable from a string alphabetic variable

    Hi everyone,

    Please find below data from a survey that asks participants to complete tasks A, B, C, D, E, F, G, H and I. The order in which these tasks were displayed to the participants was random.

    1) The variable "uid" refers to participant id.
    2) Variable "order" shows the order in which the tasks were displayed. For example, for participant 12216214, the 7th task was "A", whereas for participant 12216185 the 3rd task was "A"

    Is there a way I can create a numeric variable (say "task_number") that captures the order number of the task for each individual and task.

    For example, for scenario A, I want the variable to look like the following, see table below:
    uid order task task_number
    12216214 DECFBGAHI A 7
    12216185 BCADIEHFG A 3
    12216186 ABICHDGEF A 1
    12216247 EFDGCHBIA A 9
    12216232 FGEHDICAB A 8
    Any help on this will be highly appreciated.


    Code:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long uid str9 order str1 task
    12216214 "DECFBGAHI" "A"
    12216185 "BCADIEHFG" "A"
    12216186 "ABICHDGEF" "A"
    12216247 "EFDGCHBIA" "A"
    12216232 "FGEHDICAB" "A"
    12216214 "GHFIEADBC" "B"
    12216185 "BCADIEHFG" "B"
    12216186 "DECFBGAHI" "B"
    12216247 "HIGAFBECD" "B"
    12216232 "CDBEAFIGH" "B"
    12216214 "CDBEAFIGH" "C"
    12216185 "EFDGCHBIA" "C"
    12216186 "FGEHDICAB" "C"
    12216247 "GHFIEADBC" "C"
    12216232 "HIGAFBECD" "C"
    12216214 "BCADIEHFG" "D"
    12216185 "IAHBGCFDE" "D"
    12216186 "CDBEAFIGH" "D"
    12216247 "DECFBGAHI" "D"
    12216214 "ABICHDGEF" "D"
    12216185 "FGEHDICAB" "E"
    12216186 "GHFIEADBC" "E"
    12216247 "HIGAFBECD" "E"
    12216232 "IAHBGCFDE" "E"
           . ""          ""
    end

  • #2
    g rank = strpos(order,task)

    Comment


    • #3
      Code:
      clear 
      input long uid    str9 order    str1 task    task_number
      12216214    DECFBGAHI    A    7
      12216185    BCADIEHFG    A    3
      12216186    ABICHDGEF    A    1
      12216247    EFDGCHBIA    A    9
      12216232    FGEHDICAB    A    8
      end 
      
      foreach x in A B C D E F G H I { 
          gen num`x' = strpos(order, "`x'")
      }
      
      list order num* 
      
           +--------------------------------------------------------------------------+
           |     order   numA   numB   numC   numD   numE   numF   numG   numH   numI |
           |--------------------------------------------------------------------------|
        1. | DECFBGAHI      7      5      3      1      2      4      6      8      9 |
        2. | BCADIEHFG      3      1      2      4      6      8      9      7      5 |
        3. | ABICHDGEF      1      2      4      6      8      9      7      5      3 |
        4. | EFDGCHBIA      9      7      5      3      1      2      4      6      8 |
        5. | FGEHDICAB      8      9      7      5      3      1      2      4      6 |
           +--------------------------------------------------------------------------+

      Comment


      • #4
        Although I think I showed you something relevant, and George Ford clearly had exactly the same idea, I am still puzzled at exactly what you want. You could pack all those orders into a single variable using concatenation, but I don't see that that would be more useful, and it would not be more informative.

        Comment

        Working...
        X