Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

RE: st: RE: Atribute variable value to String


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: Atribute variable value to String
Date   Wed, 23 Feb 2011 19:47:01 +0000

I am not sure why you want this, but I hope that your first question is answered by 

. sysuse auto
(1978 Automobile Data)

. egen group = group(foreign rep78) , label
(5 missing values generated)

. su group, meanonly

. forval i = 1/`r(max)' {
  2. scalar myscalar`i' = "`: label (group) `i''"
  3. }

. scalar list
 myscalar8 = Foreign 5
 myscalar7 = Foreign 4
 myscalar6 = Foreign 3
 myscalar5 = Domestic 5
 myscalar4 = Domestic 4
 myscalar3 = Domestic 3
 myscalar2 = Domestic 2
 myscalar1 = Domestic 1

and your second question by 

. mata :

: scalar_in = st_strscalar("myscalar1")

: scalar_in
  Domestic 1

: st_strscalar("myscalar1_2", scalar_in + "stuff")

: end

. scalar list
myscalar1_2 = Domestic 1stuff
 myscalar8 = Foreign 5
 myscalar7 = Foreign 4
 myscalar6 = Foreign 3
 myscalar5 = Domestic 5
 myscalar4 = Domestic 4
 myscalar3 = Domestic 3
 myscalar2 = Domestic 2
 myscalar1 = Domestic 1

I've never used string scalars for the kind of graph problem you're addressing. Locals work fine. 

Nick 
n.j.cox@durham.ac.uk 

Lucas Ferreira Mation

Dear Nick, David and Christopher, thank you very much for the answers,
ecxactly what I needed. Bellow is the debuged code to make loops with
graphs of subsections of the data (in this case by sector) and
changing the titles in each iteration.

Two questions remain:

1) how do I directly  atribute the value of a variable in a specific
line to a scalar? (an indirect aproach would be to to atribute that
value to a labe (egen ...=group(...), label ) and then atribute that
to the scalar)

2) how do I atribute the value in a cell in a mata matrix to a Stata
scalar (I know you could bring that matrix to the database).


* schematic data
input str20 sector_code str20 sector_name Y year
	s1 "industry" 1 2000
	s1 "industry" 2 2001
	s2 "commerce" 5 2000
	s2 "commerce" 4 2001
	end

*Aproach 1: atribute sector_name to lebel of a new var (Nick Cox's sugestion):
egen sec_name_group = group(sector_name), label
egen sec_code_group = group(sector_code), label
su sec_name_group, meanonly
cd "C:\...\graphs"
forval i = 1/`r(max)' {
    twoway line Y year if[sec_name_group==`i'], title("`: label
(sec_name_group) `i' '")
	graph export y_year_`: label (sec_code_group) `i' '.wmf
	}


*Aproach 2: create dataset with the details of each graph iteration
(sector restrictions,titles, notes, options), send that information to
a matrix and loop over that matrix. This is indirect but could be very
convinient if you had several details changing per graph. For this to
work however I need  atribute the value in a cell in a mata matrix to
a stata scalar



On Tue, Feb 22, 2011 at 10:46 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> If I understand this correctly, the problem need not involve putting
> data into Mata only to have to pull them out again.
>
> There are various more direct solutions. Here is one. See also
>
> http://www.stata.com/support/faqs/data/foreach.html
>
> egen group = group(clas_cnae), label
> su group, meanonly
>
> forval i = 1/`r(max)' {
>        twoway line Y ano if group == `i', title("`: label (group) `i''")
> }
>
> You will presumably want to save those graphs too.
>
> Nick
>
> Lucas Ferreira Mation
>
>>> I have a dataset with the variables: sector_code sector_name year Y
>>> I want to create the graph line  Y year   for each sector. As there
>>> are too many sectors (500) , i want to make a loop like:
>>> global sector_list 01112 01120 // the actual list is much longer
>>> foreach s in $sector_list {
>>> preserve
>>>       keep if clas_cnae=="`s'"
>>>       twoway (line Y ano) , title ( /* SOMEHOW THE VALUE IN VARIABLE
>>> sector_name*/)
>>> restore
>>> }
>>> How can I do that? I guess my question is equivlent to: how to
>>> atribute the content of a string variable to a scalar (which I could
>>> later use in the loop)? That would be the easyest way if I knew how.
>
> [...]

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index