variable function

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

variable function

Angela Maupin

Dear All,

Is there a function that will return whether a variable is a string or numeric type? I wish to conditionally test variable type and then set missing values. Pseudo code example:

do if (vartype = string) then.
missing values ....
else if (vartype = numeric).
missing values ....
end if.


Thanks!

Angela Maupin

Reply | Threaded
Open this post in threaded view
|

Re: variable function

Bruce Weaver
Administrator
Angela Maupin wrote

Dear All,

Is there a function that will return whether a variable is a string or numeric type? I wish to conditionally test variable type and then set missing values. Pseudo code example:

do if (vartype = string) then.
missing values ....
else if (vartype = numeric).
missing values ....
end if.

Thanks!

Angela Maupin
I suspect there is a Python-based solution to this.  Nevertheless, here is a more old-fashioned approach (using native SPSS code only) that might do the trick.

   http://spsstools.net/Macros/Gems/vartype.txt

Instead of:

LIST !str.
LIST !num.

I think you could just do something like:

missing values !str ("Missing" "N/A").
missing values !num(888 999).

--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: variable function

Albert-Jan Roskam
Hi,

Untested:
import spss, spssaux

# define boolean function
def is_numeric(var):
  if var in spssaux.VariableDict(variableType="numeric").variables:
    return True
  else:
    return False

# actually use the function
if is_numeric("somevar"):
  spss.Submit("missing value somevar (-1).")
else:
  spss.Submit("missing value somevar ('NA').")

This may not work as expected for date variables. You can also put the spss.Submit steps in the function itself of course.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 4/7/10, Bruce Weaver <[hidden email]> wrote:

From: Bruce Weaver <[hidden email]>
Subject: Re: [SPSSX-L] variable function
To: [hidden email]
Date: Wednesday, April 7, 2010, 8:25 PM

Angela Maupin wrote:

>
>
>
> Dear All,
>
> Is there a function that will return whether a variable is a string or
> numeric type? I wish to conditionally test variable type and then set
> missing values. Pseudo code example:
>
> do if (vartype = string) then.
> missing values ....
> else if (vartype = numeric).
> missing values ....
> end if.
>
> Thanks!
>
> Angela Maupin
>

I suspect there is a Python-based solution to this.  Nevertheless, here is a
more old-fashioned approach (using native SPSS code only) that might do the
trick.

   http://spsstools.net/Macros/Gems/vartype.txt

Instead of:

LIST !str.
LIST !num.

I think you could just do something like:

missing values !str ("Missing" "N/A").
missing values !num(888 999).



-----
--
Bruce Weaver
bweaver@...
http://sites.google.com/a/lakeheadu.ca/bweaver/
"When all else fails, RTFM."

NOTE:  My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
--
View this message in context: http://old.nabble.com/variable-function-tp28168765p28169057.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@... (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: variable function

Jon K Peck
In reply to this post by Bruce Weaver

If you want a macro that selects variables according to type and other characteristics, take a look at the SPSSINC SELECT VARIABLES extension command available from SPSS Developer Central, www.spss.com/devcentral  (Requires at least V17 and the Python programmability plugin).

It can select based on any combination of variable type, measurement level, patterns in the name, and custom attributes from a specified list of variables or the entire dictionary.

Here is an example selecting all numeric variables and running DESCRIPTIVES on them.

SPSSINC SELECT VARIABLES MACRONAME="!numeric"
/PROPERTIES TYPE=NUMERIC.
DESCRIPTIVES !numeric.

HTH,
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Bruce Weaver <[hidden email]>
To: [hidden email]
Date: 04/07/2010 12:29 PM
Subject: Re: [SPSSX-L] variable function
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Angela Maupin wrote:
>
>
>
> Dear All,
>
> Is there a function that will return whether a variable is a string or
> numeric type? I wish to conditionally test variable type and then set
> missing values. Pseudo code example:
>
> do if (vartype = string) then.
> missing values ....
> else if (vartype = numeric).
> missing values ....
> end if.
>
> Thanks!
>
> Angela Maupin
>

I suspect there is a Python-based solution to this.  Nevertheless, here is a
more old-fashioned approach (using native SPSS code only) that might do the
trick.

 
http://spsstools.net/Macros/Gems/vartype.txt

Instead of:

LIST !str.
LIST !num.

I think you could just do something like:

missing values !str ("Missing" "N/A").
missing values !num(888 999).



-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/
"When all else fails, RTFM."

NOTE:  My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
--
View this message in context:
http://old.nabble.com/variable-function-tp28168765p28169057.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


Reply | Threaded
Open this post in threaded view
|

Re: variable function

Art Kendall
In reply to this post by Angela Maupin
The documentation mentions that SORT VARIABLES has a way to put the original order in a custom attribute, but I do not see how to do that via syntax.


Switch to the variable view and right click on <type> . click <sort ascending> and enter the name of the new custom attribute in the edit box.

Look up the first and last string variable and the first and last numeric variable.
Use that info to write your MISSING VALUES syntax.

 Sort the variables back into the original order.

Art Kendall
Social Research Consultants



On 4/7/2010 1:59 PM, Angela Maupin wrote:

Dear All,

Is there a function that will return whether a variable is a string or numeric type? I wish to conditionally test variable type and then set missing values. Pseudo code example:

do if (vartype = string) then.
missing values ....
else if (vartype = numeric).
missing values ....
end if.


Thanks!

Angela Maupin

===================== 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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: variable function

Albert-Jan Roskam
In reply to this post by Angela Maupin
Hi,

this is probably closer to what you want than what I suggested earlier.

import spss, spssaux
numvars = spssaux.VariableDict(variableType="numeric").variables
for var in spssaux.VariableDict().variables:
  if var in numvars:
    spss.Submit("missing value %s (-1)." % var)
  else:
    spss.Submit("missing value %s ('NA')." % var)

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 4/7/10, Angela Maupin <[hidden email]> wrote:

From: Angela Maupin <[hidden email]>
Subject: [SPSSX-L] variable function
To: [hidden email]
Date: Wednesday, April 7, 2010, 7:59 PM

Dear All,

Is there a function that will return whether a variable is a string or numeric type? I wish to conditionally test variable type and then set missing values. Pseudo code example:

do if (vartype = string) then.
missing values ....
else if (vartype = numeric).
missing values ....
end if.


Thanks!

Angela Maupin