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: Problem using wildcard and renaming string variables


From   Pierre Pratley <[email protected]>
To   [email protected]
Subject   Re: st: RE: Problem using wildcard and renaming string variables
Date   Tue, 23 Jul 2013 14:17:51 -0400

Thanks Matt,

Very helpful!

I already had a variable with the filenames turned string variable, so
generating vars using a wave identifier (substr(-2,3) and id(-9,7) and
then forcing a destring and replacing values outside either wave did
the trick!

Many thanks,

Pierre

On Tue, Jul 23, 2013 at 1:27 PM, Barclay Matthew (SHEFFIELD TEACHING
HOSPITALS NHS FOUNDATION TRUST) <[email protected]> wrote:
> Hi Pierre,
>
> I am not sure whether your problem relates to filenames, variable names, or observations in a particular variable "ratingR1". I think you are trying to handle observations in a particular variable "ratingR1", and these observations are actually the filenames of various files of survey data.
>
> What you want to do is to create two variables, one with only the observations (filenames) from survey wave 1 and one with only the observations from survey wave 2. You don't want the observations in either of these new variables to have the two character suffix.
>
> I have two suggestions, one helpful and one not. The unhelpful suggestion is to read an introductory text on Stata programming, or to take the web course, so that you understand the syntax. The helpful suggestion is to not use a regular expression, as in this particular case it is not necessary. Some example code that should do what you want is below.
>
>
> ******* start example
>
> /* generate wave identifier, containing just the 2 character suffix */
> gen wave_id = substr(ratingR1,-2,.)
>
> /* generate variable containing the length of ratingR1 */
> gen length_ratingR1 = length(ratingR1)
>
> /* replace this with one 2 shorter, so we can get rid of the suffix easily */
> replace length_ratingR1 = length_ratingR1-2
>
> /* get rid of suffix */
> replace ratingR1 = substr(ratingR1,1,length_ratingR1)
>
> /* generate wave 1 and wave 2 variables */
> generate ratingR1_wave1 = ratingR1 if inlist(wave_id,"01","06","09","11","13")
> generate ratingR1_wave2 = ratingR1 if inlist(wave_id,"03","05","08","12","14")
>
> ******* end example
>
>
> Hope that helps,
> Matt
>
>
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Pierre Pratley
> Sent: 23 July 2013 16:23
> To: statalist
> Subject: st: Problem using wildcard and renaming string variables
>
> Dear colleagues,
>
> I'm having problems renaming / removing string variables using a wildcard.
> I am using Stata 11.
> Basically, I have added a suffix of two numbers to filenames that are in my
> database as a string variable.
> The two numbers identifying survey waves are: suffixes 01, 06, 09, 11 and
> 13 for one round 1 and
> 03, 05, 08, 12 or 14 for round 2.
>
> I'm trying to create a new variable per round, one in which the suffixes
> are removed for round 1, and  observations are dropped for round 2, and
> vice versa.
>
> I have gone through statalist and help functions and it seems I need to use
> a -regexr extension but am having trouble with the syntax:
> drop if ratingR1 regexr(ratingR1, "*[03][05][08][12][14]$")
>
> This should drop all values in the string variable ratingR1 that end in 03,
> 05, 08, 12 and 14?
>
> And then how do I replace the values 01, 06, 09, 11 and 13?
>
> Any help would be much appreciated. Thanks.
> *
> *   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/
>
> ********************************************************************************************************************
>
> This message may contain confidential information. If you are not the intended recipient please inform the
> sender that you have received the message in error before deleting it.
> Please do not disclose, copy or distribute information in this e-mail or take any action in reliance on its contents:
> to do so is strictly prohibited and may be unlawful.
>
> Thank you for your co-operation.
>
> NHSmail is the secure email and directory service available for all NHS staff in England and Scotland
> NHSmail is approved for exchanging patient data and other sensitive information with NHSmail and GSi recipients
> NHSmail provides an email address for your career in the NHS and can be accessed anywhere
>
> ********************************************************************************************************************
>
> *
> *   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index