Announcement

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

  • Create scalar depending on a condition (involving scalars)

    Hey,

    I want to assign the starting value of my loop to a scalar. The starting value depends on two things:
    1. Which row has the xth value of a certain variable (I use "sum" in order to get information on the start of the series).
    2. Which row belongs to a certain date (here I use "su" in order to get that information).

    Whichever of these two rows is "later" in the series I want to use as starting value.

    I tried the following ("sstart" contains the information of condition 1, "r(min)" the information of condition 2):

    if r(min) > sstart {
    scalar drop sstart
    scalar sstart = r(min)
    }
    which leads to "invalid syntax"

    and

    scalar sstart2 = r(min) if (r(min) > sstart)
    which leads to "if not allowed".

    I hope my problem is clear. Thanks in advance!

    Best,
    Thomas

  • #2
    I cannot replicate your difficulty in #1:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . scalar sstart = -1
    
    . summ price
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |         74    6165.257    2949.496       3291      15906
    
    . if r(min) > sstart {
    .         scalar drop sstart
    .         scalar sstart = r(min)
    . }
    
    . display sstart
    3291
    works just fine for me.

    Now, that said, you could be encountering your problem if your -summ- command does not immediately precede the -if r(min) > sstart-. Remember that the values stored in r() are volatile: they disappear as soon as you run any other command that stores something in r(). So if there is an rclass command somewhere between your -summ- command and your attempt to reference r(min), r(min) will be undefined, and -if rt(min) > sstart= will be interpreted as -if > sstart- which is, indeed, a syntax error.

    As for your second problem, the -scalar - command does not allow -if- qualifiers. In fact, it would make no sense for it to do so. -if- qualifiers tell Stata which subset of the observations in memory are to be used in executing the command. But -scalar sstart2 = whatever- doesn't involve the observations from the data set at all, so even were -if- qualifiers allowed, they would have no effect. What you mean here, I believe, is you want Stata to check whether the current value of r(min) > sstart and then run the -scalar sstart2 = r(min)- command only when that is the case. The code for that involves an -if- command, not an -if- qualifier:

    Code:
    if r(min) > sstart {
        scalar sstart2 = r(min)
    }
    As with your first issue, this code will only work provided the corresponding -summ- command appears immediately before -if r(min) > sstart {- (or at least no other rclass commands come between them.)

    Added: As an afterthought, the -scalar drop sstart- command is not needed and accomplishes nothing.
    Last edited by Clyde Schechter; 15 Jun 2017, 09:26.

    Comment


    • #3
      Thanks for your answer Clyde! Interesting.

      My code actually consists of several nested loops. Since I'm not able to debug the code I just commented out the new parts in order to find out what caused the error. The if-part follows directly after the -summ- command.

      The code looks like this (the loops runs for all countries in the dataset and the current value is `cc'):
      sum obsN if id==`cc'

      if r(N)>40 {
      scalar smin = r(min)
      scalar smax = r(max)
      scalar sstart = r(min)+40
      su obsN if id == `cc' & date == yq(1990, 2), meanonly
      if r(min) > sstart {
      [INDENT=2]scalar sstart = r(min)[/INDENT]
      }
      ...
      Commenting out the if-part leads to the code running without any problem.

      edit:
      I found the problem. If no observation fulfills the condition of the -summ- command the variable r(min) does not exist, which leads to an error. Thanks a lot!
      Last edited by Thomas Mitterling; 15 Jun 2017, 10:01. Reason: Problem found

      Comment

      Working...
      X