help with syntax

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

help with syntax

DR VEENA Joshi
Hi,

I am trying to learn syntax and would be grateful if some one can help with
the following:


1)Insert variable VAR00001; enter the date 2005/11/24. Copy and Paste in
full column.

Rename VAR00001 as "today_date".



2)         Copy datedeath paste Death_yr.

            change Death_yr from string to numeric 4.

(datedeath variable is in the format yyyy-dd-mm. I would like this to be
only yyyy – years which can be different say from 1997 to 2003. therefore I
changed the date format to numeric with width 4. Is there any other way to
do this?



3)         In column "datedeath' replace system blanks with today date
(…./../..).

4)         Recode "datedeath" to "datedeath01"  1 = today date  (…./../..);
0 = other than today date



5)         I used the following syntax to get a cross tab.

CROSSTABS

             /TABLES=yearfirstesrd  BY age_categories

             /FORMAT= AVALUE TABLES

             /CELLS= COUNT

             /COUNT ROUND CELL.



I would like to use this table in transpose form in SPSS data. i.e. Column
should be years and data should be for each category of age in a separate
SPSS data file by name X.sav.
Reply | Threaded
Open this post in threaded view
|

Re: help with syntax

Richard Ristow
At 09:59 AM 6/18/2006, DR VEENA Joshi wrote:

>I am trying to learn syntax and would be
>grateful if some one can help with the following:

Well, you asked a lot of questions, so this is a
very long response. I've put comments in the
code, giving your questions and some remarks
about how I've answered them. This code is
tested; this is SPSS draft output.

* Test data:                                              .
LIST.

List
|-------------------------|------------------------|
|Output Created           |18-JUN-2006 16:14:13    |
|-------------------------|------------------------|
  ID age_categories yearfirstesrd datedeath

001        3            1996     2002-05-15
002        3            1992
003        1            1991
004        3            1998
005        2            1992     1999-01-26
006        3            1988     2000-06-05
007        2            1988     2003-01-01
008        1            1998     2000-05-06
009        1            1997     2000-12-23
010        3            1998     1999-02-18

Number of cases read:  10    Number of cases listed:  10


* "1)Insert variable VAR00001; enter the date 2005/11/24. .
* "  Copy and Paste in full column. Rename VAR00001 as    .
* "  'today_date'".                                       .

* Create new variable "today_date", which has value       .
* 2005/11/24 for all cases.                               .
* CAUTION:                                                .
* A. 'today_date' is an SPSS date variable. It is *not*   .
*    the string "2005/11/24", though that's how it prints,.
*    and how it looks in the Data Editor.                 .
*       You must be careful whether you want a string or  .
*    an SPSS date variable. (Usually, SPSS date variables .
*    are the better choice.)                              .
* B. This is a variable whose values are all the same.    .
*    That's rarely the best way to get what you want.     .

* In syntax, you use COMPUTE where you'd copy and paste.  .
* And you create variables with the names they'll have;   .
* you don't rename them.                                  .

* Create the variable. It's an SPSS date variable.        .
NUMERIC today_date(SDATE10).
* Give it the value November 24, 2005.                    .
COMPUTE today_date=DATE.DMY(24,11,2005).
LIST.

List
|-------------------------|------------------------|
|Output Created           |18-JUN-2006 16:14:14    |
|-------------------------|------------------------|
  ID age_categories yearfirstesrd datedeath  today_date

001        3            1996     2002-05-15 2005/11/24
002        3            1992                2005/11/24
003        1            1991                2005/11/24
004        3            1998                2005/11/24
005        2            1992     1999-01-26 2005/11/24
006        3            1988     2000-06-05 2005/11/24
007        2            1988     2003-01-01 2005/11/24
008        1            1998     2000-05-06 2005/11/24
009        1            1997     2000-12-23 2005/11/24
010        3            1998     1999-02-18 2005/11/24

Number of cases read:  10    Number of cases listed:  10


* "2) Copy datedeath paste Death_yr.                      .
* "   change Death_yr from string to numeric 4.           .
* "   (datedeath variable is in the format yyyy-dd-mm.    .
* "   I would like this to be only yyyy years.)"          .

* In syntax, you create the new variable, then use COMPUTE.
* to copy the old variable's value AND change their form. .

*  Create the new variable, as numeric.                   .
NUMERIC Death_Yr (F4).
*  Convert the first four characters of "datedeath" to a  .
*  number, and store in the new variable.                 .
COMPUTE Death_Yr = NUMBER(SUBSTR(datedeath,1,4),F4).
LIST.

List
|-------------------------|------------------------|
|Output Created           |18-JUN-2006 16:14:14    |
|-------------------------|------------------------|
  ID age_categories yearfirstesrd datedeath  today_date Death_Yr

001        3            1996     2002-05-15 2005/11/24   2002
002        3            1992                2005/11/24      .
003        1            1991                2005/11/24      .
004        3            1998                2005/11/24      .
005        2            1992     1999-01-26 2005/11/24   1999
006        3            1988     2000-06-05 2005/11/24   2000
007        2            1988     2003-01-01 2005/11/24   2003
008        1            1998     2000-05-06 2005/11/24   2000
009        1            1997     2000-12-23 2005/11/24   2000
010        3            1998     1999-02-18 2005/11/24   1999

Number of cases read:  10    Number of cases listed:  10


* "3) In column "datedeath' replace system blanks with    .
*     today date (…./../..)."                             .

* The following logic works if 'datedeath' is a STRING    .
* variable; it won't, if 'datedeath' is an SPSS date.     .
* CAUTION:                                                .
* It is unwise to modify an existing variable for any     .
* reason. It loses information, and can be badly confusing.

* The following logic works for a string but not for an   .
* You'll see that it does not use variable 'todaydate'    .
* that was created earlier.                               .

IF (datedeath eq ' ') datedeath = '2005/11/24'.

* "4) Recode "datedeath" to "datedeath01":                .
* "   1 = today date  (…./../..);                         .
* "   0 = other than today date."                         .

* The following logic works for a string but not for an   .
* SPSS date variable                                      .

NUMERIC datedeath01  (F2).
RECODE  DATEDEATH
        ('2005/11/24' = 1)
        ( ELSE        = 0) INTO datedeath01.

*  Variable 'today_date' is not listed                    .
LIST ID age_categories yearfirstesrd
        datedeath   Death_Yr datedeath01.

List
|-------------------------|------------------------|
|Output Created           |18-JUN-2006 16:14:14    |
|-------------------------|------------------------|
  ID age_categories yearfirstesrd datedeath  Death_Yr datedeath01

001        3            1996     2002-05-15   2002         0
002        3            1992     2005/11/24      .         1
003        1            1991     2005/11/24      .         1
004        3            1998     2005/11/24      .         1
005        2            1992     1999-01-26   1999         0
006        3            1988     2000-06-05   2000         0
007        2            1988     2003-01-01   2003         0
008        1            1998     2000-05-06   2000         0
009        1            1997     2000-12-23   2000         0
010        3            1998     1999-02-18   1999         0

Number of cases read:  10    Number of cases listed:  10


* "5) I would like to use this table in transpose form in .
* "   SPSS data. i.e. Column should be years and data     .
* "   should be for each category of age in a separate    .
* "   SPSS data file by name X.sav.                       .

* This is trickier logic. I don't know whether or not     .
* you'll find it easy to use.                             .

DO IF   age_categories EQ 1.
.  XSAVE OUTFILE =
      'C:\Documents and Settings\Richard\My Documents\Temporary\SPSS' +
      '2006-06-18 Joshi - help with syntax '                          +
      'Age_Cat.1.SAV'
     /KEEP = ID age_categories yearfirstesrd.
ELSE IF age_categories EQ 2.
.  XSAVE OUTFILE =
      'C:\Documents and Settings\Richard\My Documents\Temporary\SPSS' +
      '2006-06-18 Joshi - help with syntax '                          +
      'Age_Cat.2.SAV'
     /KEEP = ID age_categories yearfirstesrd.
ELSE IF age_categories EQ 3.
.  XSAVE OUTFILE =
      'C:\Documents and Settings\Richard\My Documents\Temporary\SPSS' +
      '2006-06-18 Joshi - help with syntax '                          +
      'Age_Cat.3.SAV'
     /KEEP = ID age_categories yearfirstesrd.
END IF.
EXECUTE.

GET FILE =
      'C:\Documents and Settings\Richard\My Documents\Temporary\SPSS' +
      '2006-06-18 Joshi - help with syntax '                          +
      'Age_Cat.1.SAV'.
LIST.

List
|-------------------------|------------------------|
|Output Created           |18-JUN-2006 16:14:14    |
|-------------------------|------------------------|
C:\Documents and Settings\Richard\My Documents\Temporary
   \SPSS2006-06-18 Joshi - help with syntax Age_Cat.1.SAV

  ID age_categories yearfirstesrd

003        1            1991
008        1            1998
009        1            1997

Number of cases read:  3    Number of cases listed:  3