Combine Numeric Variables

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

Combine Numeric Variables

Ryan
Dear SPSS-L,

I have a data set in which three numeric variables need to be combined into one variable. 

The three variables look like this in the data set:

x1   x2 x3
001  1  0
002  0  1
003  0  1
.    .  .
.    .  .
500  1  1

Note that x1 has leading zeros for numbers less than 100 which must remain in the combined variable. The combined variable should look like this:

x4
00110
00201
00301
.
.
50011

Any help would be appreciated. Apologies if this has been discussed previously. 

Best,

Ryan
===================== 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: Combine Numeric Variables

Bruce Weaver
Administrator
Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But I think it should do the trick.  I assume your x1 is a numeric variable formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce


Ryan Black wrote
Dear SPSS-L,

I have a data set in which three numeric variables need to be combined into
one variable.

The three variables look like this in the data set:

x1   x2 x3
001  1  0
002  0  1
003  0  1
.    .  .
.    .  .
500  1  1

Note that x1 has leading zeros for numbers less than 100 which must remain
in the combined variable. The combined variable should look like this:

x4
00110
00201
00301
.
.
50011

Any help would be appreciated. Apologies if this has been discussed
previously.

Best,

Ryan

=====================
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
--
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: Combine Numeric Variables

Ryan
Hi Bruce.Worked like a charm. Thank you. -Ryan

On Sat, Jan 16, 2016 at 2:23 PM, Bruce Weaver <[hidden email]> wrote:
Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But
I think it should do the trick.  I assume your x1 is a numeric variable
formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce



Ryan Black wrote
> Dear SPSS-L,
>
> I have a data set in which three numeric variables need to be combined
> into
> one variable.
>
> The three variables look like this in the data set:
>
> x1   x2 x3
> 001  1  0
> 002  0  1
> 003  0  1
> .    .  .
> .    .  .
> 500  1  1
>
> Note that x1 has leading zeros for numbers less than 100 which must remain
> in the combined variable. The combined variable should look like this:
>
> x4
> 00110
> 00201
> 00301
> .
> .
> 50011
>
> Any help would be appreciated. Apologies if this has been discussed
> previously.
>
> Best,
>
> Ryan
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
--
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://spssx-discussion.1045642.n5.nabble.com/Combine-Numeric-Variables-tp5731311p5731312.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: Combine Numeric Variables

PRogman
In reply to this post by Bruce Weaver
I sometimes find this type of concatenation troublesome, especially if there is missing data.
COMPUTE x4 = SUM(x1*100, x2*10, x3).
will set missing data to 0 and evaluate the sum, whereas
COMPUTE x4 = x1*100 + x2*10 + x3.
will set x4 to missing if any term is missing.
Hopefully x1 to x3 are mandatory input data and non-missing, otherwise used carefully ;-)
/PRogman


Bruce Weaver wrote
Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But I think it should do the trick.  I assume your x1 is a numeric variable formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce


Ryan Black wrote
Dear SPSS-L,

I have a data set in which three numeric variables need to be combined into
one variable.

The three variables look like this in the data set:

x1   x2 x3
001  1  0
002  0  1
003  0  1
.    .  .
.    .  .
500  1  1

Note that x1 has leading zeros for numbers less than 100 which must remain
in the combined variable. The combined variable should look like this:

x4
00110
00201
00301
.
.
50011

Any help would be appreciated. Apologies if this has been discussed
previously.

Best,

Ryan

=====================
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: Combine Numeric Variables

Bruce Weaver
Administrator
Yes, good catch.  Alternatively, one could use SUM.3 to return missing if any components are missing.

COMPUTE x4 = SUM.3(x1*100, x2*10, x3).

This has the advantage of being more explicit about what will happen if any component is missing.  With the + signs, one is depending on the syntax user to know the rules!  ;-)  


PRogman wrote
I sometimes find this type of concatenation troublesome, especially if there is missing data.
COMPUTE x4 = SUM(x1*100, x2*10, x3).
will set missing data to 0 and evaluate the sum, whereas
COMPUTE x4 = x1*100 + x2*10 + x3.
will set x4 to missing if any term is missing.
Hopefully x1 to x3 are mandatory input data and non-missing, otherwise used carefully ;-)
/PRogman


Bruce Weaver wrote
Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But I think it should do the trick.  I assume your x1 is a numeric variable formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce


Ryan Black wrote
Dear SPSS-L,

I have a data set in which three numeric variables need to be combined into
one variable.

The three variables look like this in the data set:

x1   x2 x3
001  1  0
002  0  1
003  0  1
.    .  .
.    .  .
500  1  1

Note that x1 has leading zeros for numbers less than 100 which must remain
in the combined variable. The combined variable should look like this:

x4
00110
00201
00301
.
.
50011

Any help would be appreciated. Apologies if this has been discussed
previously.

Best,

Ryan

=====================
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
--
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: Combine Numeric Variables

John F Hall
In reply to this post by Ryan

Ryan

 

That’s a natty little trick often used for data management.  Together with RECODE it can be a simple alternative to a series of IF or DO IF commands to create conditional combinations.  For instance a social services client once wanted secondary analysis of their own local authority data from a regional health and lifestyle survey* to produce a new variable combining responsibility for children with marital status:

 

freq v670 v715.

 

RESPONSIBLE FOR CHILDREN

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

Yes

432

30.6

30.6

30.6

No

953

67.5

67.5

98.2

3

26

1.8

1.8

100.0

Total

1411

100.0

100.0

 

 

 

MARITAL STATUS

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

SINGLE

526

37.3

37.5

37.5

MARRIED

467

33.1

33.3

70.8

COHABITING

73

5.2

5.2

76.0

SEPARATED

50

3.5

3.6

79.5

DIVORCED

122

8.6

8.7

88.2

WIDOWED

160

11.3

11.4

99.6

OTHER

5

.4

.4

100.0

Total

1403

99.4

100.0

 

Missing

System

8

.6

 

 

Total

1411

100.0

 

 

 

The new variable was generated by:

 

compute famstat=v670*10+v715.

recode famstat (21 24 25 26=1)(22 23=2)(11 14 15 16=3)(12 13=2)(else=sysmis).

value labels famstat

1 'Single: no kids' 2 'Couple: no kids' 3 'Single: + kids' 4 'Couple: + kids'.

freq var famstat.

 

Et voilà!

famstat

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

Single: no kids

671

47.6

48.9

48.9

Couple: no kids

535

37.9

39.0

87.8

Single: + kids

167

11.8

12.2

100.0

Total

1373

97.3

100.0

 

Missing

System

38

2.7

 

 

Total

1411

100.0

 

 

. . which was then tabulated against a range of income, diet and health measures.

 

The list has many instances of similar tricks of the trade.

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

SPSS start page:  www.surveyresearch.weebly.com/1-survey-analysis-workshop

 

* Griffiths S

Unfair Shares: an investigation of the effects the Social Security Act in Islington

Welfare Rights Unit, Islington 1989

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Ryan Black
Sent: 16 January 2016 20:46
To: [hidden email]
Subject: Re: Combine Numeric Variables

 

Hi Bruce.Worked like a charm. Thank you. -Ryan

 

On Sat, Jan 16, 2016 at 2:23 PM, Bruce Weaver <[hidden email]> wrote:

Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But
I think it should do the trick.  I assume your x1 is a numeric variable
formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce



Ryan Black wrote

> Dear SPSS-L,


>
> I have a data set in which three numeric variables need to be combined
> into
> one variable.
>
> The three variables look like this in the data set:
>
> x1   x2 x3
> 001  1  0
> 002  0  1
> 003  0  1
> .    .  .
> .    .  .
> 500  1  1
>
> Note that x1 has leading zeros for numbers less than 100 which must remain
> in the combined variable. The combined variable should look like this:
>
> x4
> 00110
> 00201
> 00301
> .
> .
> 50011
>
> Any help would be appreciated. Apologies if this has been discussed
> previously.
>
> Best,
>
> Ryan
>

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





-----
--
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://spssx-discussion.1045642.n5.nabble.com/Combine-Numeric-Variables-tp5731311p5731312.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

===================== 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: Combine Numeric Variables

Ryan
Very clever, John! Thank you, and thanks to everybody who responded. -Ryan

On Sun, Jan 17, 2016 at 1:30 AM, John F Hall <[hidden email]> wrote:

Ryan

 

That’s a natty little trick often used for data management.  Together with RECODE it can be a simple alternative to a series of IF or DO IF commands to create conditional combinations.  For instance a social services client once wanted secondary analysis of their own local authority data from a regional health and lifestyle survey* to produce a new variable combining responsibility for children with marital status:

 

freq v670 v715.

 

RESPONSIBLE FOR CHILDREN

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

Yes

432

30.6

30.6

30.6

No

953

67.5

67.5

98.2

3

26

1.8

1.8

100.0

Total

1411

100.0

100.0

 

 

 

MARITAL STATUS

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

SINGLE

526

37.3

37.5

37.5

MARRIED

467

33.1

33.3

70.8

COHABITING

73

5.2

5.2

76.0

SEPARATED

50

3.5

3.6

79.5

DIVORCED

122

8.6

8.7

88.2

WIDOWED

160

11.3

11.4

99.6

OTHER

5

.4

.4

100.0

Total

1403

99.4

100.0

 

Missing

System

8

.6

 

 

Total

1411

100.0

 

 

 

The new variable was generated by:

 

compute famstat=v670*10+v715.

recode famstat (21 24 25 26=1)(22 23=2)(11 14 15 16=3)(12 13=2)(else=sysmis).

value labels famstat

1 'Single: no kids' 2 'Couple: no kids' 3 'Single: + kids' 4 'Couple: + kids'.

freq var famstat.

 

Et voilà!

famstat

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

Single: no kids

671

47.6

48.9

48.9

Couple: no kids

535

37.9

39.0

87.8

Single: + kids

167

11.8

12.2

100.0

Total

1373

97.3

100.0

 

Missing

System

38

2.7

 

 

Total

1411

100.0

 

 

. . which was then tabulated against a range of income, diet and health measures.

 

The list has many instances of similar tricks of the trade.

John F Hall (Mr)

[Retired academic survey researcher]

 

Email:   [hidden email] 

Website: www.surveyresearch.weebly.com

SPSS start page:  www.surveyresearch.weebly.com/1-survey-analysis-workshop

 

* Griffiths S

Unfair Shares: an investigation of the effects the Social Security Act in Islington

Welfare Rights Unit, Islington 1989

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Ryan Black
Sent: 16 January 2016 20:46
To: [hidden email]
Subject: Re: Combine Numeric Variables

 

Hi Bruce.Worked like a charm. Thank you. -Ryan

 

On Sat, Jan 16, 2016 at 2:23 PM, Bruce Weaver <[hidden email]> wrote:

Hi Ryan.  I have no SPSS on this machine, so the following is untested.  But
I think it should do the trick.  I assume your x1 is a numeric variable
formatted as N3.  (If not, this definitely won't work.)

COMPUTE x4 = SUM(x1*100, x2*10, x3).
FORMATS x4 (N5). /* Keep leading zeros.
* List first 5 cases to check.
LIST VARIABLES= x1 to x4 /CASES=FROM 1 TO 5.

Cheers,
Bruce



Ryan Black wrote

> Dear SPSS-L,


>
> I have a data set in which three numeric variables need to be combined
> into
> one variable.
>
> The three variables look like this in the data set:
>
> x1   x2 x3
> 001  1  0
> 002  0  1
> 003  0  1
> .    .  .
> .    .  .
> 500  1  1
>
> Note that x1 has leading zeros for numbers less than 100 which must remain
> in the combined variable. The combined variable should look like this:
>
> x4
> 00110
> 00201
> 00301
> .
> .
> 50011
>
> Any help would be appreciated. Apologies if this has been discussed
> previously.
>
> Best,
>
> Ryan
>

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





-----
--
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://spssx-discussion.1045642.n5.nabble.com/Combine-Numeric-Variables-tp5731311p5731312.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


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