Re: Do loop

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

Re: Do loop

Maguin, Eugene
I can think of two different ways to do this but I'll bet there are others.

Way 1 is to use casestovars to restructure the file long to wide, smoking which is now smoking.1 to smoking.n. And, I assume smoking.i has the shown string values rather than 0 = no or 1 = yes. Declare smoking.1 to smoking.n a vector. Compute new.variable="same". Now use a do loop to test if smoking(i) equals smoking(i+1) for i=1 to n-1 and, if not, set new.variable="different". Now use varstocases to go backwards and which spreads new.variable across the cases.

Way 2 is to use aggregate twice (two passes). Pass 1 with mode=addvariables, break=group and compute nn=nu. Pass 2 with mode=addvariables, break=group smoking and compute ng=nu. If  ng equals nn, compute new.variable="same"; else new.variable equals "different".

Way 2 is way easier.
Gene Maguin


-----Original Message-----
From: SPSSX(r) Discussion <[hidden email]> On Behalf Of Ga??t??? S??a???
Sent: Friday, January 4, 2019 3:50 AM
To: [hidden email]
Subject: Do loop

Hello!

I am writing here because there has been a specific transformation I need to do with data but I do not really know how to do it.
I will give you an example.

Let's say we 3 groups of people, each group containing a different number of people. The first group has 6 people, the 2nd has 3, the 3rd has 5.
We measure whether they are smokers or not.

Group Smoking

Group 1 yes
Group 1 yes
Group 1 yes
Group 1 no
Group 1 yes
Group 1 yes
Group 2 yes
Group 2 no
Group 2 no
Group 3 yes
Group 3 yes
Group 3 yes
Group 3 yes
Group 3 yes

What I would like to do is make a new variable which is equal to smoking variable BUT if within the group there are different answers then it gets the value "different".
So what I'd like to do is actually this:

Group Smoking New variable

Group 1 yes different
Group 1 yes different
Group 1 yes different
Group 1 no different
Group 1 yes different
Group 1 yes different
Group 2 yes different
Group 2 no different
Group 2 no different
Group 3 yes yes
Group 3 yes yes
Group 3 yes yes
Group 3 yes yes
Group 3 yes yes


Can you please help me with this?
A lot of times I've been asked to do something like this but I'm not that good at SPSS yet.

Kind regards, Galatios

=====================
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: Do loop

Rich Ulrich
I would figure that eventually I would want to differentiate "all smokers" from
"all non-smokers".  And I might want the fractions, especially if the groups get larger.

To start, I would use AGGREGATE to get the percentage of smokers.

That is, if TEMPVAR=1 for Smoking, 0 otherwise, then PIN(tempvar, 1,1) gives
the percent of smokers. Use RECODE with INTO  to get the other variables
you need, where 0 or 100% both show "same".

--
Rich Ulrich


From: SPSSX(r) Discussion <[hidden email]> on behalf of Γαλάτιος Σιγανός <[hidden email]>
Sent: Friday, January 4, 2019 3:50 AM
To: [hidden email]
Subject: Do loop
 
Hello!

I am writing here because there has been a specific transformation I need to
do with data but I do not really know how to do it.
I will give you an example.

Let's say we 3 groups of people, each group containing a different number of
people. The first group has 6 people, the 2nd has 3, the 3rd has 5.
We measure whether they are smokers or not.

Group                   Smoking

Group 1         yes            
Group 1         yes
Group 1         yes
Group 1         no
Group 1         yes
Group 1         yes
Group 2         yes
Group 2         no     
Group 2         no
Group 3         yes
Group 3         yes
Group 3         yes
Group 3         yes
Group 3         yes

What I would like to do is make a new variable which is equal to smoking
variable BUT if within the group there are different answers then it gets
the value "different".
So what I'd like to do is actually this:

Group                   Smoking         New variable

Group 1         yes                     different      
Group 1         yes                     different      
Group 1         yes                     different      
Group 1         no                      different      
Group 1         yes                     different      
Group 1         yes                     different      
Group 2         yes                     different      
Group 2         no                      different      
Group 2         no                      different      
Group 3         yes                     yes
Group 3         yes                     yes
Group 3         yes                     yes
Group 3         yes                     yes
Group 3         yes                     yes


Can you please help me with this?
A lot of times I've been asked to do something like this but I'm not that
good at SPSS yet.

Kind regards, Galatios

=====================
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: Do loop

Bruce Weaver
Administrator
I posted a reply earlier (via Nabble), but I still cannot see it (reading via
Nabble).  Perhaps it will show up eventually.  In it, I used AGGREGATE with
the SD function to determine whether all observations within a group had the
same smoking status (SD=0) or not (SD > 0).  But Rich's comment about
perhaps finding  use for the proportion of smokers within each group later
on suggests a slight revision to that approach.  For example:

NEW FILE.
DATASET CLOSE ALL.
DATA LIST LIST / Group Smoker (2F1).
BEGIN DATA
 1 1
 1 1
 1 1
 1 0
 1 1
 1 1
 2 1
 2 0
 2 0
 3 1
 3 1
 3 1
 3 1
 3 1
END DATA.

AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /BREAK=Group
  /pSmoke=FGT(Smoker,0).
DO IF (pSmoke GT 0) AND (pSmoke LT 1).
- COMPUTE NewVar = 2.
ELSE.
- COMPUTE NewVar = Smoker.
END IF.
FORMATS NewVar (F1).
VALUE LABELS
 Smoker 0 "No" 1 "Yes" /
 NewVar
  0 "All group members are non-smokers"
  1 "All group members are smokers"
  2 "Mixed group"
.
VARIABLE LABELS
 pSmoke "Proportion of smokers within Group"
 NewVar "Smoking status of Group".
SUMMARIZE
  /TABLES=Group Smoker pSmoke NewVar
  /FORMAT=VALIDLIST NOCASENUM TOTAL LIMIT=100
  /TITLE='Case Summaries'
  /MISSING=VARIABLE
  /CELLS=NONE.

Hoping this post shows up, and hoping it helps!  

Cheers,
Bruce



Rich Ulrich wrote

> I would figure that eventually I would want to differentiate "all smokers"
> from
> "all non-smokers".  And I might want the fractions, especially if the
> groups get larger.
>
> To start, I would use AGGREGATE to get the percentage of smokers.
>
> That is, if TEMPVAR=1 for Smoking, 0 otherwise, then PIN(tempvar, 1,1)
> gives
> the percent of smokers. Use RECODE with INTO  to get the other variables
> you need, where 0 or 100% both show "same".
>
> --
> Rich Ulrich





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

--
Sent from: http://spssx-discussion.1045642.n5.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
--
Bruce Weaver
bweaver@lakeheadu.ca
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.
Reply | Threaded
Open this post in threaded view
|

Re: Do loop

David Marso-2
In reply to this post by Maguin, Eugene
I like the SD version for the simple reason that we need not know the specific values involved.

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