Hi,
I'm trying to automate the following code
gen ysr1 = . // Initialize with missing values
replace ysr1 = ysr1_actedtooyoung_0 if time == 0
replace ysr1 = ysr1_actedtooyoung_1 if time == 1
replace ysr1 = ysr1_actedtooyoung_2 if time == 2
I have 118 YSR variables and have set up a local macro but the looping isn't working and I'm not sure why. It does not return any errors.
TIA,
Kim
// Create a local macro with all the base variable names
local baseNames ysr1_actedtooyoung ysr2_alcnoparntapprvl ysr3_arguedalot ysr4_failtocomplete ysr5_enjoyedlittle ///
ysr6_likedanimals ysr7_bragged ysr8_cantconcentrate ysr9_thoughttoomuch ysr10_cantsitstill ///
ysr11_dependentonadults ysr12_feltlonely ysr13_feltconfused ysr14_criedalot ysr15_prettyhonest ///
ysr16_meantoothers ysr17_daydreamedalot ysr18_attempthurtokillself ysr19_triedtogetattention ysr20_destroyedownthings ///
ysr21_destrydothrsthngs ysr22_disobeyedparents ysr23_disobeyedatschool ysr24_noteatenwell ysr25_dntgetalongotkids ///
ysr26_nogltaftrdoingsmthng ysr27_jeolousofothers ysr28_brokerules ysr29_afrdstionsnotsch ysr30_afraidofsch ///
ysr31_afraiddosomethingbad ysr32_felthavetobeperfect ysr33_feltnoonelovesme ysr34_feltothersouttogetme ysr35_feltworthless ///
ysr36_accidentgothurt ysr37_foughtotchildren ysr38_gotmadefunof ysr39_aroundtroublesomekids ysr40_heardsoundsorvoices ///
ysr41_actedwithoutthinking ysr42_ratherbealone ysr43_liedorcheated ysr44_bitingfingernails ysr45_nervsoranxs ///
ysr46_twtchdornrvsmvmnt ysr47_nightmares ysr48_notlkdbyothrkids ysr49_dothngsbtrthnotkids ysr50_toofrfuloranxs ///
ysr51_dizzyorlightheaded ysr52_feltguilty ysr53_eatentoomuch ysr54_ovrtrdwthoutrsn ysr55_feltoverweight ///
ysr56a_achesorpains ysr56b_headaches ysr56c_nausea ysr56d_eyeproblem ysr56e_rash ysr56f_stomachache ysr56g_vomiting ///
ysr57_attackedpeople ysr58_pickdskinorothrprts ysr59_prettyfriendly ysr60_liketotrynewthings ///
ysr61_poorschoolwork ysr62_prlycrdinatedorclumsy ysr63_ratherbewitholdrkids ysr64_ratherbewithyngerkids ysr65_refusedtotalk ///
ysr66_repeatedcertainacts ysr67_runawayfromhome ysr68_screamedalot ysr69_secretive ysr70_seethngsothrscantsee ///
ysr71_slfcncseslyembrsd ysr72_setfires ysr73_workedwellwithhands ysr74_showofforfoolaound ysr75_tooshyortimid ///
ysr76_sleptlessthanmostkids ysr77_sleptmorethanmostkids ysr78_daydrmeslydstrctd ysr79_speechproblems ysr80_stoodupformyrights ///
ysr81_stolenathome ysr82_stlnfrmplcnthme ysr83_storedthingsidontneed ysr84_donestrangethings ysr85_hadstrangethoughts ///
ysr86_stubborn ysr87_suddenmoodchanges ysr88_enjydbeingwthppl ysr89_suspicious ysr90_swornordirtylanguage ///
ysr91_thoughttokillself ysr92_likdmakngothrslaugh ysr93_talkedtoomuch ysr94_teasedothersalot ysr95_hottemper ///
ysr96_thoughtofsextoomuch ysr97_threatentohurtpeople ysr98_likedhelpingothers ysr99_smokedtobacco ysr100_troublesleeping ///
ysr101_skippedschool ysr102_lowenergy ysr103_feltunhappy ysr104_louderthanotherkids ysr105_drugsnonmedpurposes ///
ysr106_fairtoothers ysr107_enjoyedgoodjoke ysr108_takelifeeasy ysr109_helpothers ysr110_wishwasoppositesex ///
ysr111_avdnvlvmntwthothrs ysr112_worriedalot
*local numVariables : word count `baseNames'
/// Loop over the range from 1 to the number of variables
forval i = 1/118 {
// Create new variable ysr`i' with missing values initially
gen ysr`i' = .
// Loop over each base name
foreach base in `baseNames' {
// Extract the full base variable name (e.g., 'ysr10_cantsitstill') to match with 'ysr`i''
local baseVarName "`base'"
// Construct the complete base variable names for different time points
local varname0 "`baseVarName'_0"
local varname1 "`baseVarName'_1"
local varname2 "`baseVarName'_2"
// Replace the value in ysr`i' based on the value of time and the corresponding varname
replace ysr`i' = `varname0' if time == 0
replace ysr`i' = `varname1' if time == 1
replace ysr`i' = `varname2' if time == 2
}
}
I'm trying to automate the following code
gen ysr1 = . // Initialize with missing values
replace ysr1 = ysr1_actedtooyoung_0 if time == 0
replace ysr1 = ysr1_actedtooyoung_1 if time == 1
replace ysr1 = ysr1_actedtooyoung_2 if time == 2
I have 118 YSR variables and have set up a local macro but the looping isn't working and I'm not sure why. It does not return any errors.
TIA,
Kim
// Create a local macro with all the base variable names
local baseNames ysr1_actedtooyoung ysr2_alcnoparntapprvl ysr3_arguedalot ysr4_failtocomplete ysr5_enjoyedlittle ///
ysr6_likedanimals ysr7_bragged ysr8_cantconcentrate ysr9_thoughttoomuch ysr10_cantsitstill ///
ysr11_dependentonadults ysr12_feltlonely ysr13_feltconfused ysr14_criedalot ysr15_prettyhonest ///
ysr16_meantoothers ysr17_daydreamedalot ysr18_attempthurtokillself ysr19_triedtogetattention ysr20_destroyedownthings ///
ysr21_destrydothrsthngs ysr22_disobeyedparents ysr23_disobeyedatschool ysr24_noteatenwell ysr25_dntgetalongotkids ///
ysr26_nogltaftrdoingsmthng ysr27_jeolousofothers ysr28_brokerules ysr29_afrdstionsnotsch ysr30_afraidofsch ///
ysr31_afraiddosomethingbad ysr32_felthavetobeperfect ysr33_feltnoonelovesme ysr34_feltothersouttogetme ysr35_feltworthless ///
ysr36_accidentgothurt ysr37_foughtotchildren ysr38_gotmadefunof ysr39_aroundtroublesomekids ysr40_heardsoundsorvoices ///
ysr41_actedwithoutthinking ysr42_ratherbealone ysr43_liedorcheated ysr44_bitingfingernails ysr45_nervsoranxs ///
ysr46_twtchdornrvsmvmnt ysr47_nightmares ysr48_notlkdbyothrkids ysr49_dothngsbtrthnotkids ysr50_toofrfuloranxs ///
ysr51_dizzyorlightheaded ysr52_feltguilty ysr53_eatentoomuch ysr54_ovrtrdwthoutrsn ysr55_feltoverweight ///
ysr56a_achesorpains ysr56b_headaches ysr56c_nausea ysr56d_eyeproblem ysr56e_rash ysr56f_stomachache ysr56g_vomiting ///
ysr57_attackedpeople ysr58_pickdskinorothrprts ysr59_prettyfriendly ysr60_liketotrynewthings ///
ysr61_poorschoolwork ysr62_prlycrdinatedorclumsy ysr63_ratherbewitholdrkids ysr64_ratherbewithyngerkids ysr65_refusedtotalk ///
ysr66_repeatedcertainacts ysr67_runawayfromhome ysr68_screamedalot ysr69_secretive ysr70_seethngsothrscantsee ///
ysr71_slfcncseslyembrsd ysr72_setfires ysr73_workedwellwithhands ysr74_showofforfoolaound ysr75_tooshyortimid ///
ysr76_sleptlessthanmostkids ysr77_sleptmorethanmostkids ysr78_daydrmeslydstrctd ysr79_speechproblems ysr80_stoodupformyrights ///
ysr81_stolenathome ysr82_stlnfrmplcnthme ysr83_storedthingsidontneed ysr84_donestrangethings ysr85_hadstrangethoughts ///
ysr86_stubborn ysr87_suddenmoodchanges ysr88_enjydbeingwthppl ysr89_suspicious ysr90_swornordirtylanguage ///
ysr91_thoughttokillself ysr92_likdmakngothrslaugh ysr93_talkedtoomuch ysr94_teasedothersalot ysr95_hottemper ///
ysr96_thoughtofsextoomuch ysr97_threatentohurtpeople ysr98_likedhelpingothers ysr99_smokedtobacco ysr100_troublesleeping ///
ysr101_skippedschool ysr102_lowenergy ysr103_feltunhappy ysr104_louderthanotherkids ysr105_drugsnonmedpurposes ///
ysr106_fairtoothers ysr107_enjoyedgoodjoke ysr108_takelifeeasy ysr109_helpothers ysr110_wishwasoppositesex ///
ysr111_avdnvlvmntwthothrs ysr112_worriedalot
*local numVariables : word count `baseNames'
/// Loop over the range from 1 to the number of variables
forval i = 1/118 {
// Create new variable ysr`i' with missing values initially
gen ysr`i' = .
// Loop over each base name
foreach base in `baseNames' {
// Extract the full base variable name (e.g., 'ysr10_cantsitstill') to match with 'ysr`i''
local baseVarName "`base'"
// Construct the complete base variable names for different time points
local varname0 "`baseVarName'_0"
local varname1 "`baseVarName'_1"
local varname2 "`baseVarName'_2"
// Replace the value in ysr`i' based on the value of time and the corresponding varname
replace ysr`i' = `varname0' if time == 0
replace ysr`i' = `varname1' if time == 1
replace ysr`i' = `varname2' if time == 2
}
}
Comment