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 |
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.
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 |
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. |
Free forum by Nabble | Edit this page |