Statalist


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

Re: st: creating a variable equal to the last number of another


From   Richard Goldstein <richgold@ix.netcom.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: creating a variable equal to the last number of another
Date   Fri, 16 Nov 2007 11:53:46 -0500

how about

gen first2=real(substr(string(code),1,2))

Rich

Oliinik, Victoria wrote:
Hi guys,
My problem is similar to Maria's, but from your command I cannot figure out how to apply it to my case.

Basically I just need to keep the first 2 digits of my variable "code".
Variable code has from 2 to 4 digits, but it is only the first 2 that I need to keep in order to sort companies for which I have US SIC codes Into sectors
"agriculture" (codes whose first 2 digits range from 01 to 09)
"mining" (codes whose first 2 digits range from 10 to 19)
"manufacturing" (codes whose first 2 digits range from 20 to 39)
"services (codes whose first 2 digits range from 40 to 99)


(as shown on http://www.census.gov/epcd/naics02/S87TON02.HTM - click on more)

Can you give me an idea on how to structure my command?

Many thanks in advance,
Victoria


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of María Paula Cacault
Sent: vrijdag 16 november 2007 14:50
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: creating a variable equal to the last number of another

Thank you very much to Michael, Phillip and Richard!
Both commands worked equally well.

Cheers,

María Paula Cacault
Asistente de Investigación
Fundación de Investigaciones Económicas Latinoamericanas
Buenos Aires, Argentina
Tel: +54 11 4314 1990 (int. 514)


-----Mensaje original-----
De: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]En nombre de Richard
Goldstein
Enviado el: Viernes, 16 de Noviembre de 2007 10:42 a.m.
Para: statalist@hsphsun2.harvard.edu
Asunto: Re: st: creating a variable equal to the last number of another


The solution below makes a number of assumptions; here is a more general one:
. sysuse auto
. gen prend=real(substr(string(price),-1,.))

Rich

Michael Foster wrote:

good morning,

here's what I'd do -- the original variable is -occ-

   gen lastdigit=occ-int(occ/10)*10

that should work--
michael

UNC School of Public Health



María Paula Cacault wrote:
Dear listers:
I have a variable (occupational code) that takes values 1 to 10000 but
always finishes in 1, 2, 3 or 4 (qualification level). I need to create a
new variable with this last numbers (qualification level) so my new variable
takes values 1 2 3 and 4 (the original variable has no puncts or commas
separating the figures).
Is there a way to do it?
Thank you very much
*
*   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/

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

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


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


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index