Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: For loop with wildcards for string suffixes

From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: For loop with wildcards for string suffixes
Date   Thu, 21 Sep 2006 10:17:49 -0400

Yes; this is what -foreach- was made for.

If you are just using suffixes, say AA through DD:

foreach suf in AA BB CC DD {
PhonePref`suf' = Preference`suf' * Phone`suf'

A neat alternative is to put the suffixes into a macro:

local suffixlist "AA BB CC DD"

foreach suf of local suffixlist {
PhonePref`suf' = Preference`suf' * Phone`suf'

Finally, you can nest these loops for more complex operations, but I'll let you figure that out.
Good luck.

At 10:05 AM 9/21/2006, you wrote:

I'm trying to figure out the syntax for generating interaction
variables using wildcards for suffixes.

I have variables PhoneAA PhoneBB PhoneCC Phone DD and PreferenceAA
PreferenceBB PreferenceCC and Preference DD.

I'd like to generate the interaction variable that would occur from a
product of these two vectors:
PhonePrefAA=PreferenceAA*PhoneAA, PhonePrefBB=PreferenceBB*PhoneBB, etc.

The problem is that I have many of these types of variables and six
suffixes, and I don't want to type out all the suffixes individually.
Is there a way I can do this using a for loop?

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index