Announcement

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

  • input a number contained in macro

    I would like to input a number that is contained in (global) macro. The following code doesn't work.

    Code:
        global N1 50
        global N2 70
        input str50 set N 
        "TEXT 1" $N1
        "TEXT 2" $N2
        end
        list
    Would there be a way to fix? Thanks in advance. In my actual code, the macro variables, N1 and N2, will include something obtained from -return- or -ereturn-. So I would like to use macro in this context.

  • #2
    This works, although it's hard to know what general form your problem takes.

    Code:
    global N1 50
    global N2 70
    clear 
    input str50 set 
    "TEXT 1" 
    "TEXT 2" 
    end
    gen N = cond(_n == 1, $N1, cond(_n == 2, $N2, .))

    Comment


    • #3
      Thanks Nick Cox for your kind answer. What I would like to do is to prepare sample selection table that looks like the following:

      Initial sample N1
      After 1st selection criteria N2
      After 2nd selection criteria N3
      ...
      ---------------------------------------
      Final sample N0

      I would like to save sample size after each step using code like this -global N1 _N. After generating macro variables of N1 to Nk and N0, I would like to prepare the sample selection table and I'll output this to excel or word file. I'm wondering if you have a better suggestion.




      Comment


      • #4
        Sorry, but I don't follow any of that.

        Comment


        • #5
          I think you are going about building your dataset the wrong way.

          In Stata 16 and later, the frame post command allows you to create a dataset in memory one observation at a time. For older versions of Stata, the postfile command offers similar capability. So instead of storing your N's into global variables, you can add successive observations with the values as they are observed, along with any other things you may want to store.
          Code:
          help frame
          help frame post
          help postfile

          Comment


          • #6
            William Lisowski Thanks William. -postfile- works very well.

            Comment

            Working...
            X