add variables while a condition is met in SPSS

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

add variables while a condition is met in SPSS

Javier Meneses
Hi, I have a list of variables that take value 1 or 2. The variables have consecutives names. As I can add variables as long as a condition is met .(ex. 1 or 2). 

I try this:


DO REPEAT x=g75 to g84.
    Do if (x=1).
    compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
    END IF.
END REPEAT. 

    
                       Javier.
===================== 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: add variables while a condition is met in SPSS

Bruce Weaver
Administrator
Does this do what you want?

COUNT Y1 = g75 to g84 (1).
COUNT Y2 = g75 to g84 (2).
COUNT Y1or2 = g75 to g84 (1 2).
FORMATS Y1 to Y1or2 (F2.0).
DESCRIPTIVES Y1 to Y1or2.



Javier Meneses wrote
Hi, I have a list of variables that take value 1 or 2. The variables have
consecutives names. As I can add variables as long as a condition is met
.(ex. 1 or 2).

I try this:


DO REPEAT x=g75 to g84.
    Do if (x=1).
    compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
    END IF.
END REPEAT.


                       Javier.

=====================
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: add variables while a condition is met in SPSS

John F Hall
In reply to this post by Javier Meneses

It’s not clear exactly what you want, but how about. . ?

 

count x = g75 to g85 (1).

freq x.

 

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

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of javier meneses
Sent: 10 February 2016 16:50
To: [hidden email]
Subject: add variables while a condition is met in SPSS

 

Hi, I have a list of variables that take value 1 or 2. The variables have consecutives names. As I can add variables as long as a condition is met .(ex. 1 or 2). 

 

I try this:

 

 

DO REPEAT x=g75 to g84.

    Do if (x=1).

    compute y=sum(x)./* I want sum (g75 to g84) only when take value=1

    END IF.

END REPEAT. 

 

    

                       Javier.

===================== 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: add variables while a condition is met in SPSS

David Marso
Administrator
In reply to this post by Javier Meneses
As Bruce and John have pointed out COUNT is quite useful.
OTOH, your syntax is incorrect.

The following will do what you seem to want.

DO REPEAT x=g75 TO g84.
IF (x EQ 1) y=SUM(y,x).
END REPEAT.

Alternatively
DO REPEAT x=g75 TO g84.
COMPUTE y= SUM(y,(x EQ 1)).
END REPEAT.

Note that SUM does NOT require y to be initialized as it would if you were to use:
COMPUTE y=y+(x EQ 1).

OTOH:  For this case you will want to look at the utility of the COUNT command.


Javier Meneses wrote
Hi, I have a list of variables that take value 1 or 2. The variables have
consecutives names. As I can add variables as long as a condition is met
.(ex. 1 or 2).

I try this:


DO REPEAT x=g75 to g84.
    Do if (x=1).
    compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
    END IF.
END REPEAT.


                       Javier.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: add variables while a condition is met in SPSS

Javier Meneses
In reply to this post by Bruce Weaver
Genial Bruce, Thank.

2016-02-10 13:04 GMT-03:00 Bruce Weaver <[hidden email]>:
Does this do what you want?

COUNT Y1 = g75 to g84 (1).
COUNT Y2 = g75 to g84 (2).
COUNT Y1or2 = g75 to g84 (1 2).
FORMATS Y1 to Y1or2 (F2.0).
DESCRIPTIVES Y1 to Y1or2.




Javier Meneses wrote
> Hi, I have a list of variables that take value 1 or 2. The variables have
> consecutives names. As I can add variables as long as a condition is met
> .(ex. 1 or 2).
>
> I try this:
>
>
> DO REPEAT x=g75 to g84.
>     Do if (x=1).
>     compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
>     END IF.
> END REPEAT.
>
>
>                        Javier.
>
> =====================
> 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/add-variables-while-a-condition-is-met-in-SPSS-tp5731479p5731480.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: add variables while a condition is met in SPSS

MLIves

Alternatively:  

Recode g75 to g84 (1=1) (2=0).

Compute y=sum(g75 to g84).

 

Yes it would be safer to recode into-but I didn’t have time to test.

Melissa

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of javier meneses
Sent: Wednesday, February 10, 2016 12:55 PM
To: [hidden email]
Subject: Re: [SPSSX-L] add variables while a condition is met in SPSS

 

Genial Bruce, Thank.

 

2016-02-10 13:04 GMT-03:00 Bruce Weaver <[hidden email]>:

Does this do what you want?

COUNT Y1 = g75 to g84 (1).
COUNT Y2 = g75 to g84 (2).
COUNT Y1or2 = g75 to g84 (1 2).
FORMATS Y1 to Y1or2 (F2.0).
DESCRIPTIVES Y1 to Y1or2.




Javier Meneses wrote

> Hi, I have a list of variables that take value 1 or 2. The variables have
> consecutives names. As I can add variables as long as a condition is met
> .(ex. 1 or 2).
>
> I try this:
>
>
> DO REPEAT x=g75 to g84.
>     Do if (x=1).
>     compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
>     END IF.
> END REPEAT.
>
>
>                        Javier.
>

> =====================
> 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/add-variables-while-a-condition-is-met-in-SPSS-tp5731479p5731480.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




This correspondence contains proprietary information some or all of which may be legally privileged; it is for the intended recipient only. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this correspondence and completely dispose of the correspondence immediately. Please notify the sender if you have received this email in error. NOTE: Messages to or from the State of Connecticut domain may be subject to the Freedom of Information statutes and regulations.

===================== 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: add variables while a condition is met in SPSS

Mark Miller
Considering that the SUM of '1's is the same as the COUNT of '1's,
I believe this can be done with COUNT, requiring neither recode nor sums.

Count  Y = g75 TO g84 (1).

On Thu, Mar 10, 2016 at 8:58 AM, Ives, Melissa L <[hidden email]> wrote:

Alternatively:  

Recode g75 to g84 (1=1) (2=0).

Compute y=sum(g75 to g84).

 

Yes it would be safer to recode into-but I didn’t have time to test.

Melissa

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of javier meneses
Sent: Wednesday, February 10, 2016 12:55 PM
To: [hidden email]
Subject: Re: [SPSSX-L] add variables while a condition is met in SPSS

 

Genial Bruce, Thank.

 

2016-02-10 13:04 GMT-03:00 Bruce Weaver <[hidden email]>:

Does this do what you want?

COUNT Y1 = g75 to g84 (1).
COUNT Y2 = g75 to g84 (2).
COUNT Y1or2 = g75 to g84 (1 2).
FORMATS Y1 to Y1or2 (F2.0).
DESCRIPTIVES Y1 to Y1or2.




Javier Meneses wrote

> Hi, I have a list of variables that take value 1 or 2. The variables have
> consecutives names. As I can add variables as long as a condition is met
> .(ex. 1 or 2).
>
> I try this:
>
>
> DO REPEAT x=g75 to g84.
>     Do if (x=1).
>     compute y=sum(x)./* I want sum (g75 to g84) only when take value=1
>     END IF.
> END REPEAT.
>
>
>                        Javier.
>

> =====================
> 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/add-variables-while-a-condition-is-met-in-SPSS-tp5731479p5731480.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




This correspondence contains proprietary information some or all of which may be legally privileged; it is for the intended recipient only. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this correspondence and completely dispose of the correspondence immediately. Please notify the sender if you have received this email in error. NOTE: Messages to or from the State of Connecticut domain may be subject to the Freedom of Information statutes and regulations.

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