trouble combining loop with do repeat?

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

trouble combining loop with do repeat?

Postby GerineL » Mon May 21, 2012 10:13 am

I am just starting to discover loop and do repeat functions.
I tried to alter a syntax found online, but it doesn't work.

The problem:
I have 6 string variables, that contain answers with both letters and numbers.
E.g.:

Var00007:
15
13.5%
I don't know
about 3
5

I want this to become:
15
13.5
.
5
3

I tried the following but it didn't work.
Also, it would be nice if there could be some sort of line that sais:

only do this for this variable if it is a string.
So that I can just enter all my variables, and the syntax will automatically detect all string varaibles and perform the syntax.
Do repeat
originals = VAR00007 VAR00008 VAR00009 VAR00010 VAR00011 VAR00012/
news = VAR00007a VAR00008a VAR00009a VAR00010a VAR00011a VAR00012a.
COMPUTE #nb=0.
LOOP news=1 TO LENGTH(RTRIM(originals)).
COMPUTE #c=RANGE(SUBSTR(originals,news,1),'0','9').
IF #c=1 #nb=#nb*10+NUMBER(SUBSTR(originals,news,1),F1).
END LOOP.
COMPUTE nb=#nb.
end repeat.
execute.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: trouble combining loop with do repeat?

Postby GerineL » Thu Jun 21, 2012 11:35 am

I really can't make anything of that answer, can you try to rephrase? Or is this not a real post?
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: trouble combining loop with do repeat?

Postby Penguin_Knight » Thu Jun 21, 2012 11:43 am

Gutnre wrote:I really can't make anything of that answer, can you try to rephrase? Or is this not a real post?
Nope, this person is a spammer. He/she just wants to get us to click the links. Let's report this.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: trouble combining loop with do repeat?

Postby GerineL » Fri Jun 22, 2012 9:08 am

I removed it...

do you happen to know the answer though?
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: trouble combining loop with do repeat?

Postby Penguin_Knight » Fri Jun 22, 2012 12:11 pm

I'm never a good user of repeat and loop... So, this is how I'd do it. You may build in the extra 2 arguments for input and output variables.

Code: Select all

STRING  Temp01 (A25).
COMPUTE Temp01=UPCASE(original).
EXECUTE.

DEFINE !Word2None (Alpha=!ENCLOSE("[","]")) .
!DO !I !IN (!Alpha)
COMPUTE Temp01=REPLACE(Temp01,!I,"").
!DOEND
RECODE Temp01 (CONVERT) INTO Final .
!ENDDEFINE .

!Word2None Alpha = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
                    "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
                    "%" "'" " "] .
EXECUTE .
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: trouble combining loop with do repeat?

Postby GerineL » Fri Jun 22, 2012 12:29 pm

so how can you do this for up to 500 variables at once?
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: trouble combining loop with do repeat?

Postby Penguin_Knight » Fri Jun 22, 2012 12:40 pm

Gutnre wrote:so how can you do this for up to 500 variables at once?
Hmm... I'll probably first build the output and input variables into the macro. And then write a Macro #2 to run the macro. Macro #1 recode the original into final. Macro #2 loop through 500 variables.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: trouble combining loop with do repeat?

Postby GerineL » Fri Jun 22, 2012 1:56 pm

I'm affraid thats a bit to advanced for me, I actually don't know anything about macros, I just happen to have some good ones saved on my computer that I can sort of change a bit so it will fit my needs.
Thanks for your input though!
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: trouble combining loop with do repeat?

Postby Penguin_Knight » Fri Jun 22, 2012 3:36 pm

Gutnre wrote:I'm affraid thats a bit to advanced for me, I actually don't know anything about macros, I just happen to have some good ones saved on my computer that I can sort of change a bit so it will fit my needs.
Thanks for your input though!
Don't let that scare you, it's actually pretty simple. I can use the "1991 U.S. General Social Survey.sav" to demonstrate. This .sav comes with standard SPSS installation.

First, here is the first macro:

Code: Select all

DEFINE PartI (MyVar = !TOKEN(1)) .
FREQUENCIES VARIABLES=!MyVar
  /ORDER=ANALYSIS.
!ENDDEFINE .
- DEFINE and !ENDDEFINE engulfs the working syntax.
- "PartI" is the name of the macro.
- (MyVar = !TOKEN(1)) has a few components:
-- "MyVar" is a macro variable, which is just an imaginary variable that we'd use to represent the later real variables.
-- = !TOKEN(1) is one of the way we can match the later real variable with this macro variable. There are a few of them. "!TOKEN" can be seen as "take whatever I give as is".
- You should be familiar the middle part. Only difference is that we replace the variable with "!MyVar". This macro variable is the exchangeable part, which later will be replaced with whatever we submit.

Usually, when we would like to run this macro, we'll use this command:

Code: Select all

PartI MyVar = sex .
EXECUTE .
Literally it just means, run the macro called PartI, and then replace "MyVar" with "sex", which is a real variable. This is the simplest form of macro, pretty silly, but it gets the point across.

However, this only does the frequency analysis for one variable at a time (sex in this case). In order to loop it many times, we can add yet another macro:

Code: Select all

DEFINE PartII (MyList = !ENCLOSE("[","]")) .
!DO !I !IN (!MyList)
PARTI MyVar = !I
!DOEND
!ENDDEFINE .
A few new components here:
- !ENCLOSE("[","]") is another way of assigning macro variable. This literally means "take whatever variables I enclose in a pair of "[" and "]" as the macro variable." Pretty useful when you have a list of variable to be processed.
- !DO !I !IN (!MyList) is my bread and butter. It's pretty much a DO loop for usual SPSS. It means "do each item (called !I here, but you can call it !J, !K, etc. it's user-defined) inside "MyList", once at a time."
- Then, we can put "PARTI MyVar = !I" to have it run our first macro.
- !DOEND closes the !DO loop.

And then, to evoke it, we can use the syntax:

Code: Select all

PartII MyList = [sex race region happy life sibs] .
EXECUTE .
This macro will then run the macro of Part I many times. Again... this is a silly example (because FREQUENCIES take multiple variables anyway), but the idea is similar when applied to more restrictive syntax/macro.

Give it some thought. And if you have explored for a while with your way and couldn't get your syntax work, welcome to come back here and I'll be happy to work on it with you. If you're interested in learning macro (I highly recommend that we all should), here is a good book:

http://szoc.tarstud.hu/upload/szoc_szak ... macros.pdf
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: trouble combining loop with do repeat?

Postby GerineL » Mon Jun 25, 2012 11:04 am

you're right, this doesn't look too complicated.
I'll toy around with this al ittle bit next weekend, it looks like it shouldn't take more than a few hours to get the basics!

Who is online

Users browsing this forum: No registered users and 1 guest

cron