# Re: Do loop

4 messages
Open this post in threaded view
|

## Re: Do loop

 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
Open this post in threaded view
|

## Re: Do loop

 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