recode using multiple variables

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

recode using multiple variables

Lacay, Phebe
Hi all,

 

I have a two part question.

 

I am not sure if I should be doing a RECODE or a COMPUTE but how do you
do a recode into a different variable the following:

 

var1 = var1 INTO newvar

If  var1 = "",  newvar = var2

 

Once newvar is created how do I recode a portion of the data.

newvar will be  6 digits.  I want to keep the first 4 and change the
last 2 accordingly:

 

All last 2 digits ending in 10 should be a 4

All last 2 digits ending in 20 should be a 2

All last 2 digits ending in 30 should be a 3

 

For example:

199510 should be 19954

200420 should be 20042

199930 should be 19993

 

Any help would be appreciated

Thanks.

====================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: recode using multiple variables

Richard Ristow
At 04:29 PM 11/9/2007, Lacay, Phebe wrote:

>I have a two part question.
>
>I am not sure if I should be doing a RECODE or a COMPUTE but how do you
>do a recode into a different variable the following:
>
>var1 = var1 INTO newvar
>If  var1 = "",  newvar = var2

So 'newvar' is to have the value of 'var1', if 'var1' is not blank;
if 'var1' is blank, 'newvar' is to have he value of 'var2'? Like
this, you mean? (Code not tested. And it only works of all three
variables already exist, and are strings.)

COMPUTE        newvar = var1.
IF  var1 EQ "" newvar = var2.

>Once newvar is created how do I recode a portion of the data.
>
>newvar will be  6 digits.  I want to keep the first 4 and change the
>last 2 accordingly:
>
>
>All last 2 digits ending in 10 should be a 4
>All last 2 digits ending in 20 should be a 2
>All last 2 digits ending in 30 should be a 3
>
>For example:
>199510 should be 19954
>200420 should be 20042
>199930 should be 19993

The question is, what do you want if the last two digits have any
other value? I'll change all values except 10, 20, and 30 into 9. I'm
using scratch variables (variables whose names begin with '#'), which
do not remain in the final file. Code not tested.

NUMERIC  #LastTwo (F2)
         /#NewLast  (F1).

COMPUTE #LastTwo = MOD(newvar,100).
RECODE  #LastTwo
          (10   = 4)
          (20   = 2)
          (30   = 3)
          (ELSE = 9) INTO #NewLast.

COMPUTE  newvar = (newvar - #LastTwo)/10 + #NewLast.

=====================
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: recode using multiple variables

Marks, Jim
In reply to this post by Lacay, Phebe
** Numeric solution.
DATA LIST FREE /var1 var2 (2f8.0).
BEGIN DATA
199510 199530
 . 200320
199030 200120
END DATA.

COMPUTE newvar1 = var1.
IF SYSMIS(var1) newvar1 = var2.

IF  newvar1 - trunc(newvar1/100)*100 = 10 newvar = trunc(newvar1/10) +
3.
IF  newvar1 - trunc(newvar1/100)*100 = 20 newvar = trunc(newvar1/10) .
IF  newvar1 - trunc(newvar1/100)*100 = 30 newvar = trunc(newvar1/10) .

ADD FILE /FILE = * /DROP newvar1.
FREQUENCIES newvar.

** Alpha solution.
DATA LIST FREE /var1 var2 (2A8).
BEGIN DATA
199510, 199530,  ,200320 ,199030 ,200120
END DATA.

STRING newvar (a5).
COMPUTE newvar = var1.
IF var1 = ' ' newvar = var2.

if substr(newvar,5,1) ='1' newvar = concat(substr(newvar,1,4),'4').
FREQUENCIES newvar.

--jim



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lacay, Phebe
Sent: Friday, November 09, 2007 3:29 PM
To: [hidden email]
Subject: recode using multiple variables

Hi all,



I have a two part question.



I am not sure if I should be doing a RECODE or a COMPUTE but how do you
do a recode into a different variable the following:



var1 = var1 INTO newvar

If  var1 = "",  newvar = var2



Once newvar is created how do I recode a portion of the data.

newvar will be  6 digits.  I want to keep the first 4 and change the
last 2 accordingly:



All last 2 digits ending in 10 should be a 4

All last 2 digits ending in 20 should be a 2

All last 2 digits ending in 30 should be a 3



For example:

199510 should be 19954

200420 should be 20042

199930 should be 19993



Any help would be appreciated

Thanks.

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

=====================
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: recode using multiple variables

Richard Ristow
In reply to this post by Richard Ristow
At 10:16 AM 11/12/2007, Lacay, Phebe wrote:

>Thank you for your help. You translated perfectly what I was asking.

Great! I'm glad.

>can you explain again what '#' represents? Does that make it a temporary step?

Yes. It makes it a 'scratch variable', in SPSS terms. Scratch
variables have properties including,
. They are not retained in the working file or any output dataset.
(So, they're handy for intermediate results you DON'T want to keep.)
. They keep their values from case to case, unless the values are
explicitly changed. (You can get this for normal variables, too, by
specifying 'LEAVE' for them.)

>I was able to do the first part however I am having difficulty with
>the modification in the second part.  Since I will be doing a
>calculation does newvar need to become a numeric?

Nope. That was my fault. I did the example calculation on the
assumption that 'newvar' was numeric, although from the first part I
should have seen that it wasn't. Here's your data, read as
characters. This is SPSS 14 draft output (WRR:not saved separately).
You can ignore variables 'should' and 'be':

File Information
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 11:52:43       |
|-----------------------------|---------------------------|
Variable Information
|--------|--------|------|---------------|------------|------------|
|Variable|Position|Label |Measurement    |Print Format|Write Format|
|        |        |      |Level          |            |            |
|--------|--------|------|---------------|------------|------------|
|newvar  |1       |<none>|Nominal        |A8          |A8          |
|--------|--------|------|---------------|------------|------------|
|should  |2       |<none>|Nominal        |A6          |A6          |
|--------|--------|------|---------------|------------|------------|
|be      |3       |<none>|Nominal        |A2          |A2          |
|--------|--------|------|---------------|------------|------------|
|wanted  |4       |<none>|Nominal        |A8          |A8          |
|--------|--------|------|---------------|------------|------------|

List
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 11:52:43       |
|-----------------------------|---------------------------|
newvar   should be wanted

199510   should be 19954
200420   should be 20042
199930   should be 19993
200140

Number of cases read:  4    Number of cases listed:  4
......................................................
Here's a solution very similar to what I gave you before, but working
with strings rather than numbers. Unfortunately, the CONCAT in the
last step is a more complicated than the addition in the last step of
the previous solution. Again, this is SPSS 14 draft output:

STRING  newvar2  (A8).
COMPUTE newvar2 = newvar /* so old value isn't lost */.

STRING   #LastTwo (A2)
         /#NewLast (A1).
NUMERIC  #Break   (F3).


COMPUTE #Break   = LENGTH(RTRIM(newvar2)) - 1.
COMPUTE #LastTwo = SUBSTR(newvar2,#Break,2).
RECODE  #LastTwo
          ('10'   = '4')
          ('20'   = '2')
          ('30'   = '3')
          (ELSE   = '9') INTO #NewLast.

COMPUTE  newvar2 = CONCAT
                    (SUBSTR(newvar2,1,#Break-1)
                    ,#NewLast).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 12:10:53       |
|-----------------------------|---------------------------|
newvar   should be wanted   newvar2

199510   should be 19954    19954
200420   should be 20042    20042
199930   should be 19993    19993
200140                      20019

Number of cases read:  4    Number of cases listed:  4
===================
APPENDIX: Test data
===================
DATA LIST LIST /newvar should be wanted (A8,A6,A2,A8).
BEGIN DATA
199510 should be 19954
200420 should be 20042
199930 should be 19993
200140
END DATA.

DISPLAY VARIABLES.

LIST.

=====================
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: recode using multiple variables

Lacay, Phebe
Thank you again.

After I ran the syntax the newvar variable appeared but no data; it says
transformations pending. I left it alone for some time and finally
closed SPSS, when I reopened the file, the data then appeared.  Is this
supposed to be a long process where I should let it continue pending? I
only have 120 cases.

Also, I am trying to merge this new variable with another file, however
it does not recognize it in the New Working Data File list. Any
suggestions?



-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Monday, November 12, 2007 11:18 AM
To: Lacay, Phebe; SPSS discussion list
Subject: RE: recode using multiple variables

At 10:16 AM 11/12/2007, Lacay, Phebe wrote:

>Thank you for your help. You translated perfectly what I was asking.

Great! I'm glad.

>can you explain again what '#' represents? Does that make it a
temporary step?

Yes. It makes it a 'scratch variable', in SPSS terms. Scratch
variables have properties including,
. They are not retained in the working file or any output dataset.
(So, they're handy for intermediate results you DON'T want to keep.)
. They keep their values from case to case, unless the values are
explicitly changed. (You can get this for normal variables, too, by
specifying 'LEAVE' for them.)

>I was able to do the first part however I am having difficulty with
>the modification in the second part.  Since I will be doing a
>calculation does newvar need to become a numeric?

Nope. That was my fault. I did the example calculation on the
assumption that 'newvar' was numeric, although from the first part I
should have seen that it wasn't. Here's your data, read as
characters. This is SPSS 14 draft output (WRR:not saved separately).
You can ignore variables 'should' and 'be':

File Information
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 11:52:43       |
|-----------------------------|---------------------------|
Variable Information
|--------|--------|------|---------------|------------|------------|
|Variable|Position|Label |Measurement    |Print Format|Write Format|
|        |        |      |Level          |            |            |
|--------|--------|------|---------------|------------|------------|
|newvar  |1       |<none>|Nominal        |A8          |A8          |
|--------|--------|------|---------------|------------|------------|
|should  |2       |<none>|Nominal        |A6          |A6          |
|--------|--------|------|---------------|------------|------------|
|be      |3       |<none>|Nominal        |A2          |A2          |
|--------|--------|------|---------------|------------|------------|
|wanted  |4       |<none>|Nominal        |A8          |A8          |
|--------|--------|------|---------------|------------|------------|

List
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 11:52:43       |
|-----------------------------|---------------------------|
newvar   should be wanted

199510   should be 19954
200420   should be 20042
199930   should be 19993
200140

Number of cases read:  4    Number of cases listed:  4
......................................................
Here's a solution very similar to what I gave you before, but working
with strings rather than numbers. Unfortunately, the CONCAT in the
last step is a more complicated than the addition in the last step of
the previous solution. Again, this is SPSS 14 draft output:

STRING  newvar2  (A8).
COMPUTE newvar2 = newvar /* so old value isn't lost */.

STRING   #LastTwo (A2)
         /#NewLast (A1).
NUMERIC  #Break   (F3).


COMPUTE #Break   = LENGTH(RTRIM(newvar2)) - 1.
COMPUTE #LastTwo = SUBSTR(newvar2,#Break,2).
RECODE  #LastTwo
          ('10'   = '4')
          ('20'   = '2')
          ('30'   = '3')
          (ELSE   = '9') INTO #NewLast.

COMPUTE  newvar2 = CONCAT
                    (SUBSTR(newvar2,1,#Break-1)
                    ,#NewLast).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |12-NOV-2007 12:10:53       |
|-----------------------------|---------------------------|
newvar   should be wanted   newvar2

199510   should be 19954    19954
200420   should be 20042    20042
199930   should be 19993    19993
200140                      20019

Number of cases read:  4    Number of cases listed:  4
===================
APPENDIX: Test data
===================
DATA LIST LIST /newvar should be wanted (A8,A6,A2,A8).
BEGIN DATA
199510 should be 19954
200420 should be 20042
199930 should be 19993
200140
END DATA.

DISPLAY VARIABLES.

LIST.

=====================
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: recode using multiple variables

Richard Ristow
At 12:42 PM 11/12/2007, Lacay, Phebe wrote:

>After I ran the syntax the newvar variable appeared but no data; it
>says transformations pending. I left it alone for some time and
>finally closed SPSS, when I reopened the file, the data then
>appeared.  Is this supposed to be a long process where I should let
>it continue pending? I only have 120 cases.

It's not a long process at all; but it won't start until it sees cause to.

The transformation program will be run when you run anything that
needs the data: a SAVE, or any procedure. Or an EXECUTE.

In your case, from the menus select

Transform > Run Pending Transformations

and it'll issue an EXECUTE, and you'll see the transformed data.

PROBABLY you saved the file when you closed SPSS, which forced the
transformations to run.

(By the way, SPSS does it this way because it's more efficient; it
can be much more efficient, for a big file. The transformations are
held until a procedure needs the data, as I said; then, the
transformations are performed and the data fed to the procedure, with
only one pass through the data file.)

>Also, I am trying to merge this new variable with another file,
>however it does not recognize it in the New Working Data File list.
>Any suggestions?

I don't have the details of menus at my fingers' ends; I use syntax,
mostly. I confess: I don't even know what you do in the menus, to get
to a "New Working Data File" list.

Can you give exactly what you've done, starting just after you've
created the new variable?

Hope to get farther later -
Richard

=====================
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: recode using multiple variables

Lacay, Phebe
Hi All,

It's me again.
How do you convert a date format from 18-MAR-1945 00:00:00 to YYYYMMDD.
Any help would be appreciated.

Thanks,
Phebe

=====================
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: recode using multiple variables

ViAnn Beadle
SPSS doesn't provide that specific date format. The nearest one is SDATE10
which is YYYY/MM/DD.

FORMAT varx(SDATE8).

should work or

go to the Variable view in the Data Editor and open the dialog under the
type column. Select the date radio button to display a list of all date
formats that SPSS provides.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lacay, Phebe
Sent: Tuesday, November 13, 2007 9:56 AM
To: [hidden email]
Subject: Re: recode using multiple variables

Hi All,

It's me again.
How do you convert a date format from 18-MAR-1945 00:00:00 to YYYYMMDD.
Any help would be appreciated.

Thanks,
Phebe

=====================
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: recode using multiple variables

Edward Boadi
In reply to this post by Lacay, Phebe
Hi Phebe ,
This should work for you.

****** Oldate and NewDate are the old and new date formats respectively
******.

STRING Yr (A4).
STRING Mon (A2).
STRING Day (A2).
STRING NewDate  (A8).

COMPUTE Yr= STRING(XDATE.YEAR(OldDate),f4.0).
COMPUTE Mon=
LPAD(LTRIM(RTRIM(STRING(XDATE.MONTH(OldDate),f2.0))),2,'0').
COMPUTE Day=LPAD(LTRIM(RTRIM(STRING(XDATE.MDAY(OldDate),f2.0))),2,'0').
COMPUTE NewDate=CONCAT(Yr,Mon,Day).
EXE.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lacay, Phebe
Sent: Tuesday, November 13, 2007 11:56 AM
To: [hidden email]
Subject: Re: recode using multiple variables


Hi All,

It's me again.
How do you convert a date format from 18-MAR-1945 00:00:00 to YYYYMMDD.
Any help would be appreciated.

Thanks,
Phebe

=====================
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: recode using multiple variables

Lacay, Phebe
Edward,

Thanks! That worked perfectly.

Phebe


-----Original Message-----
From: Edward Boadi [mailto:[hidden email]]
Sent: Tuesday, November 13, 2007 12:43 PM
To: Lacay, Phebe; [hidden email]
Subject: RE: recode using multiple variables

Hi Phebe ,
This should work for you.

****** Oldate and NewDate are the old and new date formats respectively
******.

STRING Yr (A4).
STRING Mon (A2).
STRING Day (A2).
STRING NewDate  (A8).

COMPUTE Yr= STRING(XDATE.YEAR(OldDate),f4.0).
COMPUTE Mon=
LPAD(LTRIM(RTRIM(STRING(XDATE.MONTH(OldDate),f2.0))),2,'0').
COMPUTE Day=LPAD(LTRIM(RTRIM(STRING(XDATE.MDAY(OldDate),f2.0))),2,'0').
COMPUTE NewDate=CONCAT(Yr,Mon,Day).
EXE.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lacay, Phebe
Sent: Tuesday, November 13, 2007 11:56 AM
To: [hidden email]
Subject: Re: recode using multiple variables


Hi All,

It's me again.
How do you convert a date format from 18-MAR-1945 00:00:00 to YYYYMMDD.
Any help would be appreciated.

Thanks,
Phebe

=====================
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: recode using multiple variables

ViAnn Beadle
In reply to this post by ViAnn Beadle
Does it have to be numeric? What is this purpose of this variable?

If you make it a string you can't do any arithmetic or easily change its
format to some other date format. Here's a simple way to create a string
with the slashes stripped which leaves the current variable in place and
creates a new one.

STRING alphdate(a8).
compute alphdate=REPLACE(STRING(varx,SDATE10),"/","").

The STRING function creates a string variable using the SDATE10 format
applied to varx.
The REPLACE function then strips out the slashes.
-----Original Message-----
From: Lacay, Phebe [mailto:[hidden email]]
Sent: Tuesday, November 13, 2007 10:41 AM
To: ViAnn Beadle
Subject: RE: recode using multiple variables

Can it be converted to a numeric instead of a date variable?

Phebe Lacay

908.526.1200 x 8355
[hidden email]

-----Original Message-----
From: ViAnn Beadle [mailto:[hidden email]]
Sent: Tuesday, November 13, 2007 12:30 PM
To: Lacay, Phebe; [hidden email]
Subject: RE: recode using multiple variables

SPSS doesn't provide that specific date format. The nearest one is
SDATE10
which is YYYY/MM/DD.

FORMAT varx(SDATE8).

should work or

go to the Variable view in the Data Editor and open the dialog under the
type column. Select the date radio button to display a list of all date
formats that SPSS provides.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lacay, Phebe
Sent: Tuesday, November 13, 2007 9:56 AM
To: [hidden email]
Subject: Re: recode using multiple variables

Hi All,

It's me again.
How do you convert a date format from 18-MAR-1945 00:00:00 to YYYYMMDD.
Any help would be appreciated.

Thanks,
Phebe

=====================
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: recode using multiple variables

Richard Ristow
In reply to this post by Edward Boadi
At 12:42 PM 11/13/2007, Edward Boadi wrote:

>****** Oldate and NewDate are the old and new date formats respectively
>******.
>
>STRING Yr (A4).
>STRING Mon (A2).
>STRING Day (A2).
>STRING NewDate  (A8).
>
>COMPUTE Yr= STRING(XDATE.YEAR(OldDate),f4.0).
>COMPUTE Mon=
>LPAD(LTRIM(RTRIM(STRING(XDATE.MONTH(OldDate),f2.0))),2,'0').
>COMPUTE Day=LPAD(LTRIM(RTRIM(STRING(XDATE.MDAY(OldDate),f2.0))),2,'0').
>COMPUTE NewDate=CONCAT(Yr,Mon,Day).
>EXE.

Good stuff (and, Phebe, since this works, your date is originally an
SPSS date variable, which is good), but the string formatting can be
simplified with N formats:

STRING Yr (A4).
STRING Mon (A2).
STRING Day (A2).
STRING NewDate  (A8).

COMPUTE Yr =STRING(XDATE.YEAR (OldDate),f4.0).
COMPUTE Mon=STRING(XDATE.MONTH(OldDate),N2).
COMPUTE Day=STRING(XDATE.MDAY (OldDate),N2).
COMPUTE NewDate=CONCAT(Yr,Mon,Day).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |13-NOV-2007 18:11:32       |
|-----------------------------|---------------------------|
              OLDDATE Yr   Mon Day NewDate

01-JAN-1900 00:00:00 1900 01  01 19000101
09-JUN-1944 00:00:00 1944 06  09  19440609
31-DEC-1990 00:00:00 1990 12  31  19901231
13-NOV-2007 00:00:00 2007 11  13  20071113

Number of cases read:  4    Number of cases listed:  4


For production use, one might replace 'Yr', 'Mon', and 'Day' by
scratch variables '#Yr', '#Mon', and '#Day', so they won't be in the
output (if they're not wanted there).
===================
APPENDIX: Test data
===================
DATA LIST FIXED
   /OLDDATE 01-10 (ADATE).
BEGIN DATA
01/01/1900
06/09/1944
12/31/1990
11/13/2007
END DATA.
FORMATS OLDDATE(DATETIME20).
LIST.

=====================
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: recode using multiple variables

pmulonge
Hello all, I hope you can really help me with this seemingly simple thing, for I am at my wits end.

So I have four variables and I would like to add up all the cases that have value 1 for these variables. So did the following:
RECODE M (1=1) (ELSE=SYSMIS) INTO ATL1.

RECODE H (1=2) (ELSE=SYSMIS) INTO ATL1.

RECODE F (1=3) (ELSE=SYSMIS) INTO ATL1.

RECODE G (1=4) (ELSE=SYSMIS) INTO ATL1.

Now when I run
FREQ M H F G ATL1.

There is deeply disturbing outcome: The values 1 thru 4 in ATL1 do NOT add up to the sum M=1, H=1, F=1, G=1.
In fact, I noticed that in ATL1 the number of cases for value 1 is less than the number of cases for M=1 (in the M freq table). This is also true for ATL1 values 2, 3, 4, they are just less than value 1 for H F and G respectively.

In essence, I just am loosing data when I try and sum the cases. Can anyone explain this to me or find an alternative syntax?
I tried the syntax below, which gives the exact same disturbing result when I run the FREQs.
COMPUTE NEW_VAR=ATL1D.
IF (M=1) ATL1D=1.
IF (H=1) ATL1D=1.
IF (F=1) ATL1D=1.
IF (G=1) ATL1D=1.

FREQ ATL1D.

 
P.S. Why am I doing all of this, well in the end I would like to find the union of the non-disjoint sets M H F G in my data and I have not been successfull thus far.


Pancho Mulongeni,
Research Assistant
Pharmaccess Foundation,
Namibia
Reply | Threaded
Open this post in threaded view
|

Re: recode using multiple variables

John F Hall
Try something like:

Count ones = M H F G (1) .
Freq ones .

Also have a look at the tutorials for COUNT and COMPUTE on my website

John F Hall

[hidden email]
www.surveyresearch.weebly.com





-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
pmulonge
Sent: 16 June 2011 13:46
To: [hidden email]
Subject: Re: recode using multiple variables

Hello all, I hope you can really help me with this seemingly simple thing,
for I am at my wits end.

So I have four variables and I would like to add up all the cases that have
value 1 for these variables. So did the following:
RECODE M (1=1) (ELSE=SYSMIS) INTO ATL1.

RECODE H (1=2) (ELSE=SYSMIS) INTO ATL1.

RECODE F (1=3) (ELSE=SYSMIS) INTO ATL1.

RECODE G (1=4) (ELSE=SYSMIS) INTO ATL1.

Now when I run
FREQ M H F G ATL1.

There is deeply disturbing outcome: The values 1 thru 4 in ATL1 do NOT add
up to the sum M=1, H=1, F=1, G=1.
In fact, I noticed that in ATL1 the number of cases for value 1 is less than
the number of cases for M=1 (in the M freq table). This is also true for
ATL1 values 2, 3, 4, they are just less than value 1 for H F and G
respectively.

In essence, I just am loosing data when I try and sum the cases. Can anyone
explain this to me or find an alternative syntax?
I tried the syntax below, which gives the exact same disturbing result when
I run the FREQs.
COMPUTE NEW_VAR=ATL1D.
IF (M=1) ATL1D=1.
IF (H=1) ATL1D=1.
IF (F=1) ATL1D=1.
IF (G=1) ATL1D=1.

FREQ ATL1D.


P.S. Why am I doing all of this, well in the end I would like to find the
union of the non-disjoint sets M H F G in my data and I have not been
successfull thus far.




-----
Pancho Mulongeni,
Research Assistant
Pharmaccess Foundation,
Namibia
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/recode-using-multiple-variable
s-tp1079099p4494728.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

=====================
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: recode using multiple variables

Maguin, Eugene
In reply to this post by pmulonge
Pancho,

It sounds like you want to determine how many cases have a value of 1 for
any of the variables M, H, F, or G. Some of the cases may have a value 1 for
just one of the four variables; other cases may have  a 1 for two of the
variables; and other cases may have a value of one for all of the four
variables. If that is all you want to know, the solution is very simple.
First, look at the documentation for the Count command.

The actual syntax is

Count ones=M H F G(1).

Gene Maguin



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
pmulonge
Sent: Thursday, June 16, 2011 7:46 AM
To: [hidden email]
Subject: Re: recode using multiple variables

Hello all, I hope you can really help me with this seemingly simple thing,
for I am at my wits end.

So I have four variables and I would like to add up all the cases that have
value 1 for these variables. So did the following:
RECODE M (1=1) (ELSE=SYSMIS) INTO ATL1.

RECODE H (1=2) (ELSE=SYSMIS) INTO ATL1.

RECODE F (1=3) (ELSE=SYSMIS) INTO ATL1.

RECODE G (1=4) (ELSE=SYSMIS) INTO ATL1.

Now when I run
FREQ M H F G ATL1.

There is deeply disturbing outcome: The values 1 thru 4 in ATL1 do NOT add
up to the sum M=1, H=1, F=1, G=1.
In fact, I noticed that in ATL1 the number of cases for value 1 is less than
the number of cases for M=1 (in the M freq table). This is also true for
ATL1 values 2, 3, 4, they are just less than value 1 for H F and G
respectively.

In essence, I just am loosing data when I try and sum the cases. Can anyone
explain this to me or find an alternative syntax?
I tried the syntax below, which gives the exact same disturbing result when
I run the FREQs.
COMPUTE NEW_VAR=ATL1D.
IF (M=1) ATL1D=1.
IF (H=1) ATL1D=1.
IF (F=1) ATL1D=1.
IF (G=1) ATL1D=1.

FREQ ATL1D.


P.S. Why am I doing all of this, well in the end I would like to find the
union of the non-disjoint sets M H F G in my data and I have not been
successfull thus far.




-----
Pancho Mulongeni,
Research Assistant
Pharmaccess Foundation,
Namibia
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/recode-using-multiple-variable
s-tp1079099p4494728.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

=====================
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: recode using multiple variables

Art Kendall
In reply to this post by pmulonge
What is the range of valid values for M H F G?
can you post a small made up data set that has the problem?

something like this untested syntax. Be sure to include some cases with
user missing values if your data set has some.
data list list /id (f2) M H F G (4f1).
begin data
1 1 0 3 5
2 1 1 -1 1
3 -1 2 2 2
4 3 3 -3 3
5 1 a 3 3
...
end data.
value labels M H F G
  1 'something'
  2 'something else'
...
  -1 'not applicable'
  -2 'refused to answer'
  -3 'answer unreadable'
  -4 'interviewer forgot to ask'.
missing values M H F G (lo thru 0).


[your syntax.]

Also it is usually poor practice to assign a system missing value when
you know why it is missing.  recoding to zero would be preferable if you
want to stick with recode. Sysmis means the program was unable to follow
your instructions legitimately. In the example syntax above the fifth
case has a value 'a' that the program (system) cannot read as a number
per the instructions in the format (F1). In your recode values should be
user missing because the user said to treat them that way.

Art Kendall
Social Research Consultants



On 6/16/2011 7:45 AM, pmulonge wrote:

> Hello all, I hope you can really help me with this seemingly simple thing,
> for I am at my wits end.
>
> So I have four variables and I would like to add up all the cases that have
> value 1 for these variables. So did the following:
> RECODE M (1=1) (ELSE=SYSMIS) INTO ATL1.
>
> RECODE H (1=2) (ELSE=SYSMIS) INTO ATL1.
>
> RECODE F (1=3) (ELSE=SYSMIS) INTO ATL1.
>
> RECODE G (1=4) (ELSE=SYSMIS) INTO ATL1.
>
> Now when I run
> FREQ M H F G ATL1.
>
> There is deeply disturbing outcome: The values 1 thru 4 in ATL1 do NOT add
> up to the sum M=1, H=1, F=1, G=1.
> In fact, I noticed that in ATL1 the number of cases for value 1 is less than
> the number of cases for M=1 (in the M freq table). This is also true for
> ATL1 values 2, 3, 4, they are just less than value 1 for H F and G
> respectively.
>
> In essence, I just am loosing data when I try and sum the cases. Can anyone
> explain this to me or find an alternative syntax?
> I tried the syntax below, which gives the exact same disturbing result when
> I run the FREQs.
> COMPUTE NEW_VAR=ATL1D.
> IF (M=1) ATL1D=1.
> IF (H=1) ATL1D=1.
> IF (F=1) ATL1D=1.
> IF (G=1) ATL1D=1.
>
> FREQ ATL1D.
>
>
> P.S. Why am I doing all of this, well in the end I would like to find the
> union of the non-disjoint sets M H F G in my data and I have not been
> successfull thus far.
>
>
>
>
> -----
> Pancho Mulongeni,
> Research Assistant
> Pharmaccess Foundation,
> Namibia
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/recode-using-multiple-variables-tp1079099p4494728.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
>

=====================
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: recode using multiple variables

pmulonge
Yes, the COUNT function actually finds the UNION of the four variables M F G H for me! The documentation made sense in the context of your replies. Yes, also I will avoid the practise of setting something to SYSMIS when I know it is missing.

Thank you (though I know I should avoid saying it, forgive me).
Pancho Mulongeni,
Research Assistant
Pharmaccess Foundation,
Namibia