saving selected vars given a condition

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

saving selected vars given a condition

Gonzalo Kmaid
Hi all,

I have a dataset with a bunch of vars (var1 to varN).
I am trying to save a new data file keeping only
certain vars that accomplish a condition (each
var having values greater than zero).
Manual solution is to produces freq for all
vars,  write down the vars that have values gt zero, and then use :

SAVE OUTFILE='C:\mydir\myfile.sav'
    /KEEP var1 var3 .... varN
   /COMPRESSED.

What I am looking for, is for an automated
process (not having to write down each var that I want to keep).
[I ´ll thought "Do if" conditions, but it does
not apply (this is not a "transform" operation)]

Any toughts?

TIA

Gonzalo

PD: Thanks to Albert jan Roskam and Jon Peck for previous help!!.

=====================
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
|

AW: saving selected vars given a condition

la volta statistics
Hi Gonzalo

One way of doing this is to flip the file and keep the names of the variable
in the flipped file. You then can make your conditions for the variabls to
be kept in the saving command.
After selecting the cases that have met the conditions, you can write a
syntax dynamically and include it with the INSERT command.
I made you an example. Hope this helps
Christian

*******************************
la volta statistics
Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
Ch-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email: mailto:[hidden email]
internet: http://www.lavolta.ch/



New File.
* Sample data.
DATA LIST LIST /Var1 TO Var5.
BEGIN DATA
1 3 4 6 6
2 5 8 3 3
2 3 4 3 5
1 4 3 7 1
END DATA.

* Keep Sample data.
SAVE OUTFILE='d:\temp\mydata1.sav'.


FLIP
  VARIABLES=Var1 TO Var5 .

******************************************************.
**** Start making conditions for vars to be saved ****.
* For example sum > 10 for all values per Var.
IF SUM(Var001 to Var004) > 10 Cond = 1 .
Select if (not missing (cond)).
exec.
******************************************************.


* The following aggragation is necessary to get the highest number of
* cases in the file. This will then be used to set the point
* at the end of dynamically written syntax.

Compute case = $Casenum.
Compute break = 1.
AGGREGATE
  /OUTFILE=*
  MODE=ADDVARIABLES
  /BREAK=break
  /case_max = MAX(case).

******************************************.
****  Start Write Syntax dynamically **** .
Do if $Casenum = 1.
WRITE OUTFILE='d:\temp\mysyntax.sps'
 /"SAVE OUTFILE='d:\temp\myfileSaved.sav'"
 /" /KEEP = ".
End if.
WRITE OUTFILE='d:\temp\mysyntax.sps'
 /CASE_LBL.
Do if $Casenum = case_max.
WRITE OUTFILE='d:\temp\mysyntax.sps'
 /'.'.
End if.
Exec.
******************************************.

GET FILE='d:\temp\mydata1.sav'.

* Run syntax you made dynamically.
INSERT File = "d:\temp\mysyntax.sps".


* Controll if result is correct .
* (only var2 to Var5 remain, Var1 is dropped.
get FILE='d:\temp\myfileSaved.sav'.







-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]]Im Auftrag von
Gonzalo Kmaid
Gesendet: Mittwoch, 19. März 2008 15:35
An: [hidden email]
Betreff: saving selected vars given a condition


Hi all,

I have a dataset with a bunch of vars (var1 to varN).
I am trying to save a new data file keeping only
certain vars that accomplish a condition (each
var having values greater than zero).
Manual solution is to produces freq for all
vars,  write down the vars that have values gt zero, and then use :

SAVE OUTFILE='C:\mydir\myfile.sav'
    /KEEP var1 var3 .... varN
   /COMPRESSED.

What I am looking for, is for an automated
process (not having to write down each var that I want to keep).
[I ´ll thought "Do if" conditions, but it does
not apply (this is not a "transform" operation)]

Any toughts?

TIA

Gonzalo

PD: Thanks to Albert jan Roskam and Jon Peck for previous help!!.

=====================
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

=====================
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: saving selected vars given a condition

Peck, Jon
In reply to this post by Gonzalo Kmaid
Easy to do with programmability.  This assumes that all numeric variables are to be tested.  Generalization after the program.  This program requires the spssaux, spssdata, and namedtuple modules from SPSS Developer Central (www.spss.com/devcentral) as well as the Python Plug-in.  It should work with any SPSS version  >= 14.0.1

begin program.
import spss, spssaux, spssdata

vardict=spssaux.VariableDict(variableType='numeric')
keep = set()
curs = spssdata.Spssdata(indexes=vardict.variables)

for case in curs:
  for i, v in enumerate(case):
    if v is not None and v > 0:
      keep.add(vardict[i].VariableName)
curs.CClose()
spss.Submit("SAVE OUTFILE='C:/temp/nonzero.sav' /keep " + " ".join(keep))
end program.

To specify a specific variable list to test, you could, for example add this line right after the vardict line:
vardict = spssaux.VariableDict(indexes=vardict.range(start="bdate", end="prevexp"))
to test bdate TO prevexp
or a specific list of names could be given for indexes.

This keeps only the variables passing the test, but you could also add other variables to keep in the SAVE command above.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Gonzalo Kmaid
Sent: Wednesday, March 19, 2008 8:35 AM
To: [hidden email]
Subject: [SPSSX-L] saving selected vars given a condition

Hi all,

I have a dataset with a bunch of vars (var1 to varN).
I am trying to save a new data file keeping only
certain vars that accomplish a condition (each
var having values greater than zero).
Manual solution is to produces freq for all
vars,  write down the vars that have values gt zero, and then use :

SAVE OUTFILE='C:\mydir\myfile.sav'
    /KEEP var1 var3 .... varN
   /COMPRESSED.

What I am looking for, is for an automated
process (not having to write down each var that I want to keep).
[I ´ll thought "Do if" conditions, but it does
not apply (this is not a "transform" operation)]

Any toughts?

TIA

Gonzalo

PD: Thanks to Albert jan Roskam and Jon Peck for previous help!!.

=====================
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

=====================
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