spssinc trans result: a "TO" issue

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

spssinc trans result: a "TO" issue

raw

Dear all, I checked the spssinc trans help, about using “TO” such as in nomal SPSS sintax, but I cannot figure it out how exactly it works.

My aim is looping in different origin variables such as var1 to var10 and having 10 different new variables as output.

Is that possible to do it in an expression such as

spssinc trans result=haswhite1 to haswhite2  
/VARIABLES var1 var2
/formula "haswhitespace(<>)".            

Or have I to repeat the call?




the whole code:

data list list / var1(A10) var2 (A10).
begin data
"aa aa" "bbb "
"aaa a" "b b"
"a aaaaaa" "b       b"
end data.



begin program.
import re
def haswhitespace(arg):

   return len(re.findall(r"\s", arg.rstrip()))
end program.  


spssinc trans result=haswhite1 to haswhite2  
/VARIABLES var1 var2
/formula "haswhitespace(<>)".  


ps I know that I could do some traditional macro stuff, but I would like to clarify this issue, in order to exploit more advanced Python stuff with the spssinc trans function
Reply | Threaded
Open this post in threaded view
|

Re: spssinc trans result: a "TO" issue

Jon Peck
Yes, you can use TO in the VARIABLES subcommand of SPSSINC TRANS, but the expanded list will be passed to a single call of the formula expression.  To process the whole block iteratively, you need to do two things:
1) provide a set of result variables either as an explicit list or using TO as you have done.  For example,
RESULT = haswhite1 to haswhite10.
From the syntax help for RESULT:

TO can be used in the result list with the following rules.

  • If the variables in TO exist, TO is expanded in the usual way to that list of variables in file order.
  • If the variables do not exist, and they have the same root and numerical suffixes, they are expanded in numerical order. For example, a b c01 to c03 d expands to a b c01 c02 c03 d if c01 and c03 do not exist. Either both or neither of the TO variables must exist. Any preexisting variables in the expanded list will be replaced.
You can use a single TYPE code (or default to  numeric) to be applied to all the output variables.

2) Write your function to process and return a list.  For example:

begin program.
import re
def haswhitespace(*args):

   return [len(re.findall(r"\s", item.rstrip() )) for item in args]
end program.

Notice that this uses *args in the function signature, which accepts any number of variables as a list.

HTH,
Jon Peck

On Wed, Apr 6, 2016 at 2:39 AM, raw <[hidden email]> wrote:
Dear all, I checked the spssinc trans help, about using “TO” such as in
nomal SPSS sintax, but I cannot figure it out how exactly it works.

My aim is looping in different origin variables such as var1 to var10 and
having 10 different new variables as output.

Is that possible to do it in an expression such as

spssinc trans result=haswhite1 to haswhite2
/VARIABLES var1 var2
/formula "haswhitespace(<>)".

Or have I to repeat the call?




the whole code:

data list list / var1(A10) var2 (A10).
begin data
"aa aa" "bbb "
"aaa a" "b b"
"a aaaaaa"      "b       b"
end data.



begin program.
import re
def haswhitespace(arg):

   return len(re.findall(r"\s", arg.rstrip()))
end program.


spssinc trans result=haswhite1 to haswhite2
/VARIABLES var1 var2
/formula "haswhitespace(<>)".


ps I know that I could do some traditional macro stuff, but I would like to
clarify this issue, in order to exploit more advanced Python stuff with the
spssinc trans function




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/spssinc-trans-result-a-TO-issue-tp5731872.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: spssinc trans result: a "TO" issue

Andy W
In reply to this post by raw
The multiple variable input for SPSSINC TRANS does not call the function for each variable - e.g. calls haswhitespace(var1) and then haswhitespace(var2). It passes all of the variables simultaneously to the function, i.e. haswhitespace(var1,var2). If you edit haswhitespace to take multiple arguments and return a list, the SPSSINC TRANS statement will work as expected. Example below.

*******************************************************************.
data list list / var1(A10) var2 (A10).
begin data
"aa aa" "bbb "
"aaa a" "b b"
"a aaaaaa" "b       b"
end data.
DATASET NAME test.

BEGIN PROGRAM Python.
import re
def haswhitespace2(*args):
   return [len(re.findall(r"\s", i.rstrip())) for i in args]

#Example use
x = ["x x","xx "," x x x"]
print haswhitespace(*x)
END PROGRAM.

spssinc trans result=haswhite1 to haswhite2  
/VARIABLES var1 var2
/formula "haswhitespace2(<>)".  
*******************************************************************.

Note you can also write functions to take one list as input, and then in the function call for SPSSINC TRANS use [<>], see https://andrewpwheeler.wordpress.com/2015/05/07/passing-arguments-to-spssinc-trans-2/ for an example. Here you could equivalently use "hasewhitespace2(*[<>])" to do the unpacking from a list input.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/