Stata: Generating variables in a loop using tuples local macro

user2700264

I need to generate all possible tuples of the integer numbers 1,2,3,4 (with exactly 2 items in each tuple).Then, I need to generate a set of variables that would correspond to the resulting six tuples. Each variable name should contain a reference to a tuple and the value of each variable should be a string version of a tuple itself, as illustrated below:

+--------+--------+--------+--------+--------+--------+
| var_12 | var_13 | var_14 | var_23 | var_24 | var_34 |
+--------+--------+--------+--------+--------+--------+
|     12 |     13 |     14 |     23 |     24 |     34 |
+--------+--------+--------+--------+--------+--------+

While the tuples are generated by using the tuples user-written command (for details, see http://ideas.repec.org/c/boc/bocode/s456797.html), I am stumbling with generating new variables and assigning values to them in a loop. The code looks as follows and results in a syntax error which presumably stems from using local tuples macros incorrectly, and I would greatly appreciate if someone could help me solving it.

tuples 1 2 3 4, display min(2) max(2)

forval i = 1/`ntuples' {
    gen v`i'=`tuple`i''  
    rename v`i' var_`tuple`i''
}
Nick Cox

tuples is a user-written command from SSC. Over at www.statalist.org you would be expected to explain where it comes from, and that's a very good idea here too.

In your case, you want say integers such as 12 to represent a tuple such as "1 2" but the latter looks malformed to Stata when you are creating a numeric variable. Stata certainly won't elide the space(s) even if all characters presented otherwise are numeric. So you need to do that explicitly. At the same name giving a variable one name and then promptly renaming it can be compressed.

forval i = 1/`ntuples' {
    local I : subinstr local tuple`i' " " "", all 
    gen var_`I' = `I' 
}

Creating a string variable for the tuple with space included would make part of that unnecessary, but the space is still not allowed in the variable name:

forval i = 1/`ntuples' {
    local I : subinstr local tuple`i' " " "_", all      
    gen var_`I' = "`tuple`i''"  
}

If this is the whole of your problem, it would have been quicker to write out 6 generate statements! If this is a toy problem representative of something larger, watch out that say "1 23" and "12 3" would both be mapped to "123", so eliding the spaces is unambiguous only with single digit integers; hence the appeal of holding strings as such.

I am still curious how holding the same tuple in every observation of a variable is a good idea; perhaps your larger purpose would be better met by using string scalars or the local macros themselves.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stata: using local variables to build a program in Stata

From Dev

Stata local macro not defined

From Dev

Generating sums of variables with loops in Stata

From Dev

Generating sums of variables with loops in Stata

From Dev

How to subset a local macro in Stata

From Dev

R equivalent of Stata's for-loop over local macro list of stubnames

From Dev

Calculating year dummy variables using a for loop (foreach) in Stata

From Dev

Generating variables in a for loop javascript

From Dev

Dropping various string variables in a loop in Stata

From Dev

How do I insert newline characters into Stata macro variables?

From Dev

Generating Javascript variables using php

From Dev

creating a list of tuples using for loop

From Dev

creating a list of tuples using for loop

From Dev

Stata: Using loops for keeping and reshaping a subset of variables

From Dev

Generating dummies in Stata

From Dev

Using macro variables in datastep arguments

From Dev

In Stata, how can I append to a local varlist during a loop?

From Dev

Matching on multiple variables; SAS macro do loop when using _n_ as a variable

From Dev

generating tuples using n-lists with itertools.product

From Dev

Generating a dict from a list of tuples using dict() builtin

From Dev

Local variables defined inside loop

From Dev

Excel macro loop using VBS

From Dev

How to loop macro variables into SAS macro function efficietly

From Dev

Missing } in less generating dynamic classes using loop

From Dev

Generating a dynamic nested JSON using for loop in python

From Dev

Generating random visual noise using For loop

From Dev

Issues using a local label in a macro in MASM

From Dev

Using multiple variables in a for loop

From Dev

Join string variables in pairs of two using loops in Stata

Related Related

  1. 1

    Stata: using local variables to build a program in Stata

  2. 2

    Stata local macro not defined

  3. 3

    Generating sums of variables with loops in Stata

  4. 4

    Generating sums of variables with loops in Stata

  5. 5

    How to subset a local macro in Stata

  6. 6

    R equivalent of Stata's for-loop over local macro list of stubnames

  7. 7

    Calculating year dummy variables using a for loop (foreach) in Stata

  8. 8

    Generating variables in a for loop javascript

  9. 9

    Dropping various string variables in a loop in Stata

  10. 10

    How do I insert newline characters into Stata macro variables?

  11. 11

    Generating Javascript variables using php

  12. 12

    creating a list of tuples using for loop

  13. 13

    creating a list of tuples using for loop

  14. 14

    Stata: Using loops for keeping and reshaping a subset of variables

  15. 15

    Generating dummies in Stata

  16. 16

    Using macro variables in datastep arguments

  17. 17

    In Stata, how can I append to a local varlist during a loop?

  18. 18

    Matching on multiple variables; SAS macro do loop when using _n_ as a variable

  19. 19

    generating tuples using n-lists with itertools.product

  20. 20

    Generating a dict from a list of tuples using dict() builtin

  21. 21

    Local variables defined inside loop

  22. 22

    Excel macro loop using VBS

  23. 23

    How to loop macro variables into SAS macro function efficietly

  24. 24

    Missing } in less generating dynamic classes using loop

  25. 25

    Generating a dynamic nested JSON using for loop in python

  26. 26

    Generating random visual noise using For loop

  27. 27

    Issues using a local label in a macro in MASM

  28. 28

    Using multiple variables in a for loop

  29. 29

    Join string variables in pairs of two using loops in Stata

HotTag

Archive