Announcement

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

  • Make variable to scalars if string-condition is fulfilled

    Dear Statalist,

    I wish to store the values of a variable as scalars if a string-condition is fulfilled.

    My dataset looks something like this and consists of one numeric and one string variable.

    system value
    a 6
    b 5
    c 10
    d 10

    So in this case, I wish to generate totally 4 scalars, accordingly:


    if system=="a" {
    scalar a=value
    }
    if system=="b" {
    scalar b=value
    }
    if system=="c" {
    scalar c=value
    }
    if system=="d" {
    scalar d=value
    }

    However, it seems that Stata only stores the first scalar.

    scalar list
    a = 6


    My scalar list should look like this:

    a = 6
    b = 5
    c = 10
    c = 10


    Any suggestions?


  • #2
    I assume the last scalar in your example should be d = 10. First, let's create your dataset with the dataex package described in section 12 of the FAQ.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 system float value
    "a" 6
    "b" 5
    "c" 10
    "d" 10
    end
    The if commands in your code evaluate only the first observation. Because the first observation has an "a" in the variable system, only the first scalar command is executed. Instead, you can use the commands below.
    Code:
    levelsof system, local(strings)
    foreach s of local strings {
      sum value if system == "`s'", meanonly
      scalar `s' = r(mean)
    }
    You have now four scalars.
    Code:
    . scalar list
             d =         10
             b =          5
             a =          6
             c =         10
    By the way, are you sure you want scalars and not macros? The difference is explained in [P] scalar, which you can access by clicking on the link directly under "Title" in
    Code:
    help scalar
    In future posts, please use CODE tags for Stata commands and output, it makes everything easier to read.
    Last edited by Friedrich Huebler; 06 May 2017, 06:06.

    Comment


    • #3
      Perhaps the following will do what you want.
      Code:
      generate a = value if system=="a"
      generate b = value if system=="b"
      generate c = value if system=="c"
      generate d = value if system=="d"
      or more generally, if you have more possible values of system
      Code:
      foreach v in a b c d e f g h i j k {
          generate `v' = value if system=="`v'"
      }
      The version of if that you used is not the version you need; see help if for a description of the version used in my code, and help ifcmd for the version you used.

      Added in edit: not quite awake yet in this time zone, i missed that you want scalars but the lesson about if remains valid.
      Last edited by William Lisowski; 06 May 2017, 06:11.

      Comment


      • #4
        Friedrich and William, many thanks for your suggestions. Both works like a charm.

        Comment

        Working...
        X