# Compute variable if only one case listed

12 messages
Open this post in threaded view
|

## Compute variable if only one case listed

 Hello everybody. I am trying to calculate a new variable with SPSS 25. My data set contains 2 variables. The first is called "ID" containing a nominal identifier of the subject. The second ist called "var1" and contains ordinal data attributed to the ID. Of note, there might be one or more cases for each ID, i.e. the variable ID might list a given identifier once or more. My aim is to calculate varnew in a way that data are the same like in var1, except the "value in var1 = 1" AND the "respective ID is listed only once". Then, varnew should list the value 101 instead.   I thought it might be helpful to see an illustration. Please find attached. Please note that varnew was not calculated, but edited just to illustrate the aim. The original dataset is considerably bigger (~5000 cases) and IDs are more complex. Thanks to everybody who tries to find an answer. -- 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
Open this post in threaded view
|

## Re: Compute variable if only one case listed

 What you illustrate does not seem to be useful in any obvious way, but it is not hard to do, with flexibility for variation. Use AGGREGATE to add onto each record the count for ID as n_ID Then, for your exact example, IF  (n_ID eq 1) and (var1 eq 1)   newvar= 101. If you want similar counts for other values, you can compute newvar = 10*ID + var1      instead of setting newvar to 101, 102, 103. -- Rich Ulrich From: SPSSX(r) Discussion <[hidden email]> on behalf of Gunnar <[hidden email]> Sent: Wednesday, July 15, 2020 6:32 AM To: [hidden email] <[hidden email]> Subject: Compute variable if only one case listed   Hello everybody. I am trying to calculate a new variable with SPSS 25. My data set contains 2 variables. The first is called "ID" containing a nominal identifier of the subject. The second ist called "var1" and contains ordinal data attributed to the ID. Of note, there might be one or more cases for each ID, i.e. the variable ID might list a given identifier once or more. My aim is to calculate varnew in a way that data are the same like in var1, except the "value in var1 = 1" AND the "respective ID is listed only once". Then, varnew should list the value 101 instead.  I thought it might be helpful to see an illustration. Please find attached. Please note that varnew was not calculated, but edited just to illustrate the aim. The original dataset is considerably bigger (~5000 cases) and IDs are more complex. Thanks to everybody who tries to find an answer. -- 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 ===================== 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: Compute variable if only one case listed

 Thank you very much for this information, Rich. I was not aware of this solution. FYI: For me it is useful, because the dataset I have to analyze is based on a bug, where the user was able to select a specification without selecting the main category and vice versa. Recoding the variable allows me to identify each case where solely the main category was selected, enabling further analysis. Everything else is nothing but plus and minus. So once again, thank you! Gunnar -- 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
Open this post in threaded view
|

## Re: Compute variable if only one case listed

Open this post in threaded view
|

## Re: Compute variable if only one case listed

 Cannibalizing Bruce' syntax. NEW FILE. DATASET CLOSE ALL. DATA LIST LIST / ID (A1) var1(F1) Want (f1). BEGIN DATA "A" 1 1 "A" 2 2 "B" 1 0 "C" 3 1 "C" 2 2 "D" 2 0 END DATA. * ===============  Pasted syntax from a few clicks . * Identify Duplicate Cases. SORT CASES BY ID(A). MATCH FILES   /FILE=*   /BY ID   /FIRST=PrimaryFirst   /LAST=PrimaryLast. DO IF (PrimaryFirst). COMPUTE  MatchSequence=1-PrimaryLast. ELSE. COMPUTE  MatchSequence=MatchSequence+1. END IF. LEAVE  MatchSequence. FORMATS  MatchSequence (f7). MATCH FILES   /FILE=*   /DROP=PrimaryFirst PrimaryLast. VARIABLE LABELS  MatchSequence 'Sequential count of matching cases'. VARIABLE LEVEL  MatchSequence (SCALE). FREQUENCIES VARIABLES=MatchSequence. * ==================== end Pasted syntax. VALUE LABELS MatchSequence 0    'Single occurrence of ID'. LIST. ----- Art Kendall Social Research Consultants -- 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 Art Kendall Social Research Consultants
Open this post in threaded view
|

## Re: Compute variable if only one case listed

 In reply to this post by Bruce Weaver Here is just a simple note on code practices.  I don't use DO-IF when I can avoid it by an initialization to the ELSE value. Consider Bruce's code - * To get the desired result, replace that IF with this DO-IF. DO IF (n_ID eq 1) and (var1 eq 1).   COMPUTE RUnewvar = 101. ELSE.    COMPUTE RUnewvar = var1. END IF. I find it more readable in two lines - COMPUTE RUnewvar = var1. IF  (n_ID eq 1) and (var1 eq 1)  RUnewvar = 101. Also: Dave Marso showed us how to initialize and get a result in one line, which is easy to read once you get use to seeing it that way - COMPUTE RUnewvar = var1 + ( (n_ID eq 1) and (var1 eq 1) )*100. -- Rich Ulrich ===================== 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: Compute variable if only one case listed

 But note, Rich, that on the other hand the first variant is 4 operations (3 boolean and 1 arithmetic) while the other two are 5 operations (3 boolean and 2 arithmetic). I may expect that on millions of cases (or millions of loops) the first block is just slightly faster. 18.07.2020 1:44, Rich Ulrich пишет: Here is just a simple note on code practices.  I don't use DO-IF when I can avoid it by an initialization to the ELSE value. Consider Bruce's code - * To get the desired result, replace that IF with this DO-IF. DO IF (n_ID eq 1) and (var1 eq 1).   COMPUTE RUnewvar = 101. ELSE.    COMPUTE RUnewvar = var1. END IF. I find it more readable in two lines - COMPUTE RUnewvar = var1. IF  (n_ID eq 1) and (var1 eq 1)  RUnewvar = 101. Also: Dave Marso showed us how to initialize and get a result in one line, which is easy to read once you get use to seeing it that way - COMPUTE RUnewvar = var1 + ( (n_ID eq 1) and (var1 eq 1) )*100. -- Rich Ulrich ===================== 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: Compute variable if only one case listed

 The returned value of zero From is not the exactly literal value 101 the OP asked for. Identifying case IDs that are NOT duplicated, is related to finding case IDs that ARE duplicated. I do not believe that a GUI-only approach is productive in the long run. I do believe that knowing what generalized operations are available in drafting syntax is productive. Ideally, the people implementing try to maximize both user-interface quality and machine efficiency.   How many millions of cases would it take before execution time differences between approaches would be measured in minutes? P.S.  It seems that many questions on this list come from people who are "newbies". ----- Art Kendall Social Research Consultants -- 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 Art Kendall Social Research Consultants
Open this post in threaded view
|

## Re: Compute variable if only one case listed

 In reply to this post by Kirill Orlov When examining the performance of different computational formulas, there are a lot of factors other than the actual evaluation time for the arithmetic operations to consider even ignoring parsing time and other setup of the formulas.  Just the writing of the result to the active file probably swamps the computation time in most cases.  In addition, formulas might be rewritten into a computationally more efficient form by the interpreter sometimes, although there is limited scope for this in Statistics, since numbers are all double precision floating point values and rewriting could change the result a bit.I did a little very casual benchmarking to see if there is a detectable difference. I generated a million case file of random numbers and compared execution times, excluding setup, of a formula with one multiplication to one with twenty.  I only did a dozen replications, but here is the result.Mean for one:  1.3354  Mean for twenty:  1.5212.  Times are in seconds, so for a million cases the mean difference is about 0.2 seconds.  But standard deviations are .592 and .542, and the t test sig level is .43.So, at least on this simple test, the variation from run to run swamps the difference between the formulas.  Obviously this could be explored much further, but the simple message is that clarity in the code will likely swamp any difference in run times.  I do prefer the clarify of the one-line version of the code to the other forms.On Sat, Jul 18, 2020 at 3:31 AM Kirill Orlov <[hidden email]> wrote: But note, Rich, that on the other hand the first variant is 4 operations (3 boolean and 1 arithmetic) while the other two are 5 operations (3 boolean and 2 arithmetic). I may expect that on millions of cases (or millions of loops) the first block is just slightly faster. 18.07.2020 1:44, Rich Ulrich пишет: Here is just a simple note on code practices.  I don't use DO-IF when I can avoid it by an initialization to the ELSE value. Consider Bruce's code - * To get the desired result, replace that IF with this DO-IF. DO IF (n_ID eq 1) and (var1 eq 1).   COMPUTE RUnewvar = 101. ELSE.    COMPUTE RUnewvar = var1. END IF. I find it more readable in two lines - COMPUTE RUnewvar = var1. IF  (n_ID eq 1) and (var1 eq 1)  RUnewvar = 101. Also: Dave Marso showed us how to initialize and get a result in one line, which is easy to read once you get use to seeing it that way - COMPUTE RUnewvar = var1 + ( (n_ID eq 1) and (var1 eq 1) )*100. -- Rich Ulrich ===================== 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 -- Jon K Peck[hidden email] ===================== 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: Compute variable if only one case listed

 Indenting, comments, variable labels, and value labels enhance clarity. Variable names such as "YearlyIncome" rather than "YI", "NameBlank" rather than "test1", "PointOfSale" rather than "PS'  should only add trivial time to execution. These things are also a major help in redrafting syntax so that it comes closer to what you intend. YMMV but I try to write as if I were explaining to a beginner. That some times using more lines for a statement. And sometimes more statements. ----- Art Kendall Social Research Consultants -- 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 Art Kendall Social Research Consultants