Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: how to implement a dictionary (aka map, look-up table)


From   jcoveney@bigplanet.com
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: how to implement a dictionary (aka map, look-up table)
Date   Sun, 16 Dec 2012 04:46:59 -0000

Andrin Foster wrote:

I'm trying to set a value based on observation variables matching
relevant dictionary entries.
I've tried to implement a dictionary amongst the variables in Stata,
however, I can't compare my results to the dictionary because the
dictionary entries and results are counted as different observations.

[example omitted]

What is the proper function or data format to achieve my goal?

I'm using Stata 12 on Mac OS 10.7.5

----------------------------------------------------------------------
----------

I suggest something like -merge- using separate datasets, similar to
the example shown below.

Otherwise, there are a couple of dictionary commands or functions in
Stata.  One is the user-written command -vlookup- (locate and install
it by typing "findit vlookup" at the command line).  The other one is
Mata's -asarray()- and -hash()- functions, which are formal
dictionary functions, but they're going to be more work to use, and
are probably overkill for your particular problem.

Joseph Coveney

. clear *

. set more off

. 
. // Lookup table
. input byte(X Y Z) str5 MetaValue

            X         Y         Z  MetaValue
  1. 1 1 1 "Bob"
  2. 1 1 2 "Ted"
  3. 1 1 3 "Carol"
  4. 1 1 4 "Alice"
  5. end

. tempfile Lookup

. quietly save "`Lookup'"

. drop _all

. 
. // Probe
. input byte(X Y Z)

            X         Y         Z
  1. 1 1 2
  2. 1 1 4
  3. 2 2 0
  4. 1 1 1
  5. 1 1 3
  6. end

. 
. merge m:1 X Y Z using "`Lookup'", nogenerate noreport

. quietly replace MetaValue = "No hits" if missing(MetaValue)

. list, noobs separator(0) abbreviate(20)

  +-----------------------+
  | X   Y   Z   MetaValue |
  |-----------------------|
  | 1   1   1         Bob |
  | 1   1   2         Ted |
  | 1   1   3       Carol |
  | 1   1   4       Alice |
  | 2   2   0     No hits |
  +-----------------------+

. 
. exit

end of do-file


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index