Looping Through Atypical Variable Names

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Looping Through Atypical Variable Names

Jim Moffitt
I wrote the following syntax to recode some existing contiguous
variables into some new variables.
 
NUMERIC qbv7.1.1 TO qbv7.1.7 (f1.0)
RECODE q7.1.1 TO q7.1.7 (sysmis=1) (else=2) INTO qbv7.1.1 TO qbv7.1.7.
VALUE LABELS qbv7.1.1 TO qbv 7.1.7 1 "Not Chosen" 2 "Chosen".
EXECUTE.
 
The code runs well, but I have to run it 95 times and I don't know how
to create the loop that will address a different set of 7 contiguous
variables every time the loop is run.
 
Unfortunately, I have to loop through variable sets that are defined not
by the first numerical character in the variable name, but by the
second.
 
So the first loop would create variables qbv7.1.1 TO qbv7.1.7 and recode
variables q7.1.1 TO q7.1.7 into qbv7.1.1 TO qbv7.1.7.
The next loop would create variables qbv7.2.1 TO qbv7.2.7 and recode
variables q7.2.1 TO q7.2.7 into qbv7.2.1 TO qbv7.2.7.
The next loop would create variables qbv7.3.1 TO qbv7.3.7 and recode
variables q7.3.1 TO q7.3.7 into qbv7.3.1 TO qbv7.3.7.
The final loop would create variables qbv7.95.1 TO qbv7.95.7 and recode
variables q7.95.1 TO q7.95.7 into qbv7.95.1 TO qbv7.95.7.
 
How would I write the code to loop through this process 95 times when
its the second numeric character in the variable names that changes with
each iteration?
 
Thanks for the help.
Reply | Threaded
Open this post in threaded view
|

Re:Looping Through Atypical Variable Names

Jerabek Jindrich
Hello Jim,

a macro could do.
Something like this (not tested!)

DEFINE !RE ().
!do !n = 1 !to 95
!LET !OldStart = !concat("q7.",!n,".1")
!LET !OldEnd = !concat("q7.",!n,".7")
!LET !NewStart = !concat("qbv7.",!n,".1")
!LET !NewEnd = !concat("qbv7.",!n,".7")
RECODE !OldStart TO !OldEnd (sysmis=1) (else=2) INTO !NewStart TO !NewEnd
VALUE LABELS !NewStart TO !NewEnd 1 "Not Chosen" 2 "Chosen".
!doend
!ENDDEFINE.

!RE.

HTH
Jindra

> ------------ Původní zpráva ------------
> Od: Jim Moffitt <[hidden email]>
> Předmět: Looping Through Atypical Variable Names
> Datum: 02.11.2006 19:26:44
> ----------------------------------------
> I wrote the following syntax to recode some existing contiguous
> variables into some new variables.
>
> NUMERIC qbv7.1.1 TO qbv7.1.7 (f1.0)
> RECODE q7.1.1 TO q7.1.7 (sysmis=1) (else=2) INTO qbv7.1.1 TO qbv7.1.7.
> VALUE LABELS qbv7.1.1 TO qbv 7.1.7 1 "Not Chosen" 2 "Chosen".
> EXECUTE.
>
> The code runs well, but I have to run it 95 times and I don't know how
> to create the loop that will address a different set of 7 contiguous
> variables every time the loop is run.
>
> Unfortunately, I have to loop through variable sets that are defined not
> by the first numerical character in the variable name, but by the
> second.
>
> So the first loop would create variables qbv7.1.1 TO qbv7.1.7 and recode
> variables q7.1.1 TO q7.1.7 into qbv7.1.1 TO qbv7.1.7.
> The next loop would create variables qbv7.2.1 TO qbv7.2.7 and recode
> variables q7.2.1 TO q7.2.7 into qbv7.2.1 TO qbv7.2.7.
> The next loop would create variables qbv7.3.1 TO qbv7.3.7 and recode
> variables q7.3.1 TO q7.3.7 into qbv7.3.1 TO qbv7.3.7.
> The final loop would create variables qbv7.95.1 TO qbv7.95.7 and recode
> variables q7.95.1 TO q7.95.7 into qbv7.95.1 TO qbv7.95.7.
>
> How would I write the code to loop through this process 95 times when
> its the second numeric character in the variable names that changes with
> each iteration?
>
> Thanks for the help.
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Looping Through Atypical Variable Names

Jim Moffitt
In reply to this post by Jim Moffitt
Jerabek:
Thank you so much. The code did not run on my first attempt, but I placed a period at the end of the line that precedes the VALUE LABELS line and it ran beautifully. I really appreciate your help!
Jim

-----Original Message-----
From: Jerabek Jindrich [mailto:[hidden email]]
Sent: Thursday, November 02, 2006 2:44 PM
To: Moffitt, James (West)
Cc: [hidden email]
Subject: Re:Looping Through Atypical Variable Names

Hello Jim,

a macro could do.
Something like this (not tested!)

DEFINE !RE ().
!do !n = 1 !to 95
!LET !OldStart = !concat("q7.",!n,".1")
!LET !OldEnd = !concat("q7.",!n,".7")
!LET !NewStart = !concat("qbv7.",!n,".1") !LET !NewEnd = !concat("qbv7.",!n,".7") RECODE !OldStart TO !OldEnd (sysmis=1) (else=2) INTO !NewStart TO !NewEnd VALUE LABELS !NewStart TO !NewEnd 1 "Not Chosen" 2 "Chosen".
!doend
!ENDDEFINE.

!RE.

HTH
Jindra

> ------------ Původní zpráva ------------
> Od: Jim Moffitt <[hidden email]>
> Předmět: Looping Through Atypical Variable Names
> Datum: 02.11.2006 19:26:44
> ----------------------------------------
> I wrote the following syntax to recode some existing contiguous
> variables into some new variables.
>
> NUMERIC qbv7.1.1 TO qbv7.1.7 (f1.0)
> RECODE q7.1.1 TO q7.1.7 (sysmis=1) (else=2) INTO qbv7.1.1 TO qbv7.1.7.
> VALUE LABELS qbv7.1.1 TO qbv 7.1.7 1 "Not Chosen" 2 "Chosen".
> EXECUTE.
>
> The code runs well, but I have to run it 95 times and I don't know how
> to create the loop that will address a different set of 7 contiguous
> variables every time the loop is run.
>
> Unfortunately, I have to loop through variable sets that are defined
> not by the first numerical character in the variable name, but by the
> second.
>
> So the first loop would create variables qbv7.1.1 TO qbv7.1.7 and
> recode variables q7.1.1 TO q7.1.7 into qbv7.1.1 TO qbv7.1.7.
> The next loop would create variables qbv7.2.1 TO qbv7.2.7 and recode
> variables q7.2.1 TO q7.2.7 into qbv7.2.1 TO qbv7.2.7.
> The next loop would create variables qbv7.3.1 TO qbv7.3.7 and recode
> variables q7.3.1 TO q7.3.7 into qbv7.3.1 TO qbv7.3.7.
> The final loop would create variables qbv7.95.1 TO qbv7.95.7 and
> recode variables q7.95.1 TO q7.95.7 into qbv7.95.1 TO qbv7.95.7.
>
> How would I write the code to loop through this process 95 times when
> its the second numeric character in the variable names that changes
> with each iteration?
>
> Thanks for the help.
>
>
>