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

Re: st: save the value of a string variable into a macro


From   Roger Newson <[email protected]>
To   [email protected]
Subject   Re: st: save the value of a string variable into a macro
Date   Thu, 15 Jul 2004 19:28:17 +0100

At 19:07 15/07/2004, Jean-Benoit Hardouin wrote:
In my mail, you must replace "I want to save a specific values of the variable "nomcom" into a variable, for example," by "I want to save a specific values of the variable "nomcom" into a macro, for example,"
JB Hardouin

Jean-Benoit Hardouin a �crit:


I have a file like that :

. list depcom nomcom in 1/5

+-----------------------------+
| depcom nomcom |
|-----------------------------|
1. | 18001 Acheres |
2. | 18002 Ainay-le-Vieil |
3. | 18003 Les Aix-d'Angillon |
4. | 18004 Allogny |
5. | 18005 Allouis |
+-----------------------------+

Where depcom is an id. I want to save a specific values of the variable "nomcom" into a variable, for example, I want to obtain :
. di "`mymacro'"
Archeres
To save the value of a variable in a specific observation into a macro, use subscripting. For instance, to save the value of -nomcom- in observation 42 into a macro -mymacro-, type

local mymacro=nomcom[32]

To save the value corresponding to a specific value of the ID variable -depcom-, for instance 18003, define a temporary variable and proceed as follows:

tempvar tv1
gene `tv1'=_n if depcom==18003
summ `tv1', meanonly
local index=r(min)
local mymacro=nomcom[`index']
drop `tv1'

This will set -mymacro- to the value of -nomcom- in the first observation for which -depcom- is equal to 18003, ie to " Les Aix-d'Angillon".

I hope this helps.

Roger


--
Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom

Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605
Email: [email protected]
Website: http://www.kcl-phs.org.uk/rogernewson

Opinions expressed are those of the author, not the institution.


*
* 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