SPSSforum.com is the largest free SPSS help resource worldwide.
On this SPSS forum you are sure to get an answer to your question!
Join the forum and let other SPSS users help you.
Post subject: Syntax to create new dataset with subset of variables
Posted: Fri Mar 05, 2010 2:01 am
Joined: Fri Mar 05, 2010 1:56 am Posts: 2
A few times recently I've wanted to create a new dataset using only a subset of the variables out of a very large one. Rather than going through and copying and pasting the variables I want, is there syntax to do this? I'm familiar with the 'write outfile' command, but this doesn't seem to save the important variable information, like type of data, value labels, etc.
Post subject: Re: Syntax to create new dataset with subset of variables
Posted: Fri Mar 05, 2010 5:46 am
Moderator
Joined: Mon Apr 27, 2009 2:19 am Posts: 278 Location: Melbourne, Australia, University
Hi plr2
You could try the SPSS base menu options Data>Select Cases>Var1=1>Select output-copy selected cases to a new dataset.
A simple syntax equivalent would be DATASET COPY Subset1. DATASET ACTIVATE Subset1. FILTER OFF. USE ALL. SELECT IF (V1 = 1). EXECUTE.
==================================
More complex syntax is below
In SPSS>File>New>Syntax and copy the syntax below.
Adapt code to your own variable names.
1. The key changes are to "grpvar" and change values 1 2 3 e.g. to !SplitDataSets 11 12 13/. 2. The key name changes are to "XVar" and "YVar".
/* Created by Analyst Techy 03 Mar 2010
/* 1. Divide the dataset into smaller datasets
/* Splits a dataset up by a grouping variable e.g. grpvar /* for this syntax to work you have to have a numeric variable named grpvar /* The grp values are 1, 2,3 within the variable grpvar /* Assuming a recent version of SPSS /* Open SPSS sav file before running
DEFINE !SplitDataSets (!POS !CHAREND ('/'))
!DO !gv !IN (!1)
!LET !SepSets = !CONCAT('SepDataSet',!gv)
DATASET COPY !SepSets. DATASET ACTIVATE !SepSets. FILTER OFF. USE ALL. SELECT IF (grpvar = !gv). EXECUTE.
!DOEND
!ENDDEFINE.
!SplitDataSets 1 2 3/.
=============================
Even more advanced...
You could adapt the code above and get the PC working for you, but you'd have to use VBA script and set up a cursor to traverse the SPSS dataset. If you are familiar with PL/SQL you probably no what I'm talking about. Please email me for tips on doing this in SPSS.
_________________ Analyst Techy
NOTES: 1. It is not ethical for me to provide complete answers to assignment/work solutions. 2. Any solutions I provide on this forum are free of charge, donations go towards the maintenance of the unaffiliated SPSS forum.
But I want a .sav with all of the variable information included for all of the cases. Doesn't seem like the syntax you've provided will do that. Am I missing something?
Post subject: Re: Syntax to create new dataset with subset of variables
Posted: Fri Mar 19, 2010 4:58 am
Moderator
Joined: Mon Apr 27, 2009 2:19 am Posts: 278 Location: Melbourne, Australia, University
Hi
Sorry about the delay in getting back to you.
I don't think WRITE OUTFILE is appropriate for what you want to achieve, I use WRITE OUTFILE for creating libraries for macros. I'd use SAVE OUTFILE instead.
/* In your syntax use-make sure you have the full stop at the end of the code. SAVE OUTFILE='dito.sav' /KEEP=id maps etc.
_________________ Analyst Techy
NOTES: 1. It is not ethical for me to provide complete answers to assignment/work solutions. 2. Any solutions I provide on this forum are free of charge, donations go towards the maintenance of the unaffiliated SPSS forum.
Users browsing this forum: No registered users and 1 guest
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum