Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: creating set-theoretic variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: creating set-theoretic variables
Date   Thu, 15 Dec 2005 22:10:06 -0000

This yields to an adaptation of a basic 
trick used in -selectvars- from SSC. 
The main idea is just to express 
the integers from 0 to 2^(#variables) - 1
as binary strings from all zeros to 
all ones. This leans on -inbase-, 
which is "undocumented", but has a help file. 

*! NJC 1.0.0 15 Dec 2005 
program myboolean      
	// e.g. A, B, C, D to abcd ... ABCD       
	version 8 
	syntax varlist  
	tokenize `varlist' 
	local nvars : word count `varlist'

	// i = 1, ..., 2^# variables
	qui forval i = 1/`=2 ^`: word count `varlist''' { 
		// binary representation of i - 1  
		inbase 2 `= `i' - 1' 
		local ibin : di %0`nvars'.0f `r(base)' 
		
		// initialise name and expression 
		local name 
		local exp "min(" 
		
		forval j = 1 / `nvars' { 
			local char = substr("`ibin'",`j',1) 
			if `char' { 
				local name "`name'``j''" 
				local exp  "`exp'``j''," 
			}	
			else {
				local lcj = lower("``j''") 
				local name "`name'`lcj'" 
				local exp  "`exp'1-``j''," 
			}	
		}
		
		// we have a trailing comma: as each arg is <= 1, 
		// closing with a 2 is safe 
		gen `name' = `exp'2) 
	}
end 

[email protected] 

Steve Vaisey
 
> I have a question.  I am working with a dataset with 4 
> variables, A, B, 
> C, and D, which vary continously from 0 to 1.  These 
> variables represent 
> set theoretic conditions (i.e., they are "fuzzy set" values).  What I 
> would like to do is combine them into 16 new variables, each of which 
> represents a Boolean "ideal type."  Capital letters represent the 
> presence of the set and lowercase represents the absence of the set.  
> But since we're dealing with fuzzy sets (not Boolean sets), 
> "trueness" = 
> X and "falseness" = (1-X).  I hope that's enough background.  Anyway, 
> here's what I'm doing manually:
> 
> gen ABCD = min(A,B,C,D)
> gen ABCd = min(A,B,C,(1-D))
> gen ABcd = min(A,B,(1-C),(1-D))
> etc.
> 
> Is there an easier way to do this so that Stata takes these 4 
> and makes 
> 16 new variables?  Doing it manually isn't so bad when you 
> have 3 or 4 
> variables, but with 5 or 6 (32 or 64 combinations), it gets tiresome.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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