|
Hello,
I need to create a string variable that shows name of the variable which has the lowest value. I have 256 numeric variables: var1 to var256. The value of the new string variable should look like "var1" or "var33". I am a newbie to macros and failed hundred times of trial. Any suggestions would be appreciated. Thanks! Hui ===================== 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 |
|
copy, paste, and run this example syntax into a newly opened instance of
SPSS. if it does what you want adapt it to your data. if not please describe in more detail what you want to do. * make up some data. new file. set seed = 20080925. input program. vector var (256,f1). loop id = 1 to 50. loop #k = 1 to 256. compute var(#k) = rnd(rv.uniform(0,100.5)). end loop. end case. end loop. end file. end input program. formats id (f3) var1 to var256 (f2). *adapt this to your data. compute minimum = min.1(var1 to var256). *run this syntax once to get from the frequencies *the largest number of variables that are *at the minimum value for the case. string hitvar1 hitvar2 hitvar3 (A6). compute hits = 0. do repeat v= var1 to var256/iv=1 to 256. do if v = minimum. do if hits eq 0. compute hits=hits+1. compute hitvar1 = concat('var',string(iv,f3)). else if hits=1. compute hits=hits+1. compute hitvar2 = concat('var',string(iv,f3)). else if hits=2. compute hits=hits+1. compute hitvar3 = concat('var',string(iv,f3)). else. compute hits=hits+1. end if. end if. end repeat. frequencies vars = minimum hits hitvar1 to hitvar3. Art Kendall Social Research Consultants Hui Zhao wrote: > Hello, > I need to create a string variable that shows name of the variable > which has the lowest value. I have 256 numeric variables: var1 to > var256. The value of the new string variable should look like "var1" > or "var33". I am a newbie to macros and failed hundred times of trial. > Any suggestions would be appreciated. Thanks! > > > Hui > > ===================== > 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
Art Kendall
Social Research Consultants |
|
Maybe I'm missing something, but in the case where the variables are already conveniently named with the same prefix and a consecutive numeric suffix:
string minvar (a6). compute minvar="var1". compute #min=var1. vector v=var1 to var256. loop #i=2 to 256. do if v(#i) < #min. compute #min=v(#i). compute minvar=concat("var", ltrim(string(#i, f3))). end if. end loop. This should find the FIRST variable with the lowest value, but won't tell you if there are others with that same lowest value. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Art Kendall Sent: Thursday, September 25, 2008 11:27 AM To: [hidden email] Subject: Re: Find variable with the lowest value for each case copy, paste, and run this example syntax into a newly opened instance of SPSS. if it does what you want adapt it to your data. if not please describe in more detail what you want to do. * make up some data. new file. set seed = 20080925. input program. vector var (256,f1). loop id = 1 to 50. loop #k = 1 to 256. compute var(#k) = rnd(rv.uniform(0,100.5)). end loop. end case. end loop. end file. end input program. formats id (f3) var1 to var256 (f2). *adapt this to your data. compute minimum = min.1(var1 to var256). *run this syntax once to get from the frequencies *the largest number of variables that are *at the minimum value for the case. string hitvar1 hitvar2 hitvar3 (A6). compute hits = 0. do repeat v= var1 to var256/iv=1 to 256. do if v = minimum. do if hits eq 0. compute hits=hits+1. compute hitvar1 = concat('var',string(iv,f3)). else if hits=1. compute hits=hits+1. compute hitvar2 = concat('var',string(iv,f3)). else if hits=2. compute hits=hits+1. compute hitvar3 = concat('var',string(iv,f3)). else. compute hits=hits+1. end if. end if. end repeat. frequencies vars = minimum hits hitvar1 to hitvar3. Art Kendall Social Research Consultants Hui Zhao wrote: > Hello, > I need to create a string variable that shows name of the variable > which has the lowest value. I have 256 numeric variables: var1 to > var256. The value of the new string variable should look like "var1" > or "var33". I am a newbie to macros and failed hundred times of trial. > Any suggestions would be appreciated. Thanks! > > > Hui > > ===================== > 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 |
|
Thank you, Oliver!
Art's soluction is more safe, I think. But you give a simpler one and it also works perfect for my data because there is no more than one variable has the lowest value based on the results generated from Art's syntax. However, the cases with system missing data (missing for all variables in my case) all receive value "var1" from your syntax while it gets system missing in Art's solution. Thank again both of you! Best, Hui On Thu, Sep 25, 2008 at 3:39 PM, Oliver, Richard <[hidden email]> wrote: > Maybe I'm missing something, but in the case where the variables are already conveniently named with the same prefix and a consecutive numeric suffix: > > string minvar (a6). > compute minvar="var1". > compute #min=var1. > vector v=var1 to var256. > loop #i=2 to 256. > do if v(#i) < #min. > compute #min=v(#i). > compute minvar=concat("var", ltrim(string(#i, f3))). > end if. > end loop. > > This should find the FIRST variable with the lowest value, but won't tell you if there are others with that same lowest value. > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Art Kendall > Sent: Thursday, September 25, 2008 11:27 AM > To: [hidden email] > Subject: Re: Find variable with the lowest value for each case > > copy, paste, and run this example syntax into a newly opened instance of > SPSS. > if it does what you want adapt it to your data. > if not please describe in more detail what you want to do. > > * make up some data. > new file. > set seed = 20080925. > input program. > vector var (256,f1). > loop id = 1 to 50. > loop #k = 1 to 256. > compute var(#k) = rnd(rv.uniform(0,100.5)). > end loop. > end case. > end loop. > end file. > end input program. > formats id (f3) var1 to var256 (f2). > > *adapt this to your data. > compute minimum = min.1(var1 to var256). > *run this syntax once to get from the frequencies > *the largest number of variables that are > *at the minimum value for the case. > string hitvar1 hitvar2 hitvar3 (A6). > compute hits = 0. > do repeat v= var1 to var256/iv=1 to 256. > do if v = minimum. > do if hits eq 0. > compute hits=hits+1. > compute hitvar1 = concat('var',string(iv,f3)). > else if hits=1. > compute hits=hits+1. > compute hitvar2 = concat('var',string(iv,f3)). > else if hits=2. > compute hits=hits+1. > compute hitvar3 = concat('var',string(iv,f3)). > else. > compute hits=hits+1. > end if. > end if. > end repeat. > frequencies vars = minimum hits hitvar1 to hitvar3. > > Art Kendall > Social Research Consultants > > Hui Zhao wrote: >> Hello, >> I need to create a string variable that shows name of the variable >> which has the lowest value. I have 256 numeric variables: var1 to >> var256. The value of the new string variable should look like "var1" >> or "var33". I am a newbie to macros and failed hundred times of trial. >> Any suggestions would be appreciated. Thanks! >> >> >> Hui >> >> ===================== >> 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 > -- Hui Zhao, Ph.D. Research & Evaluation Specialist I Edvantia, Inc. 304.347.0411 (work) 360.232.3794 (cell) ===================== 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 |
|
In reply to this post by Hui Zhao
Shlom
Here is a solution base on restructuring the file , it will handle any number of variables that are equal to lowest value . * title 'find variable with the lowest value for each case' . * make up some data. new file. set seed = 20080925. input program. vector var (256,f1). loop id = 1 to 50. loop #k = 1 to 256. compute var(#k) = rnd(rv.uniform(2,1002.5)). end loop. end case. end loop. end file. end input program. formats id (f3) var1 to var256 (f2). execute . VARSTOCASES /MAKE vars FROM var1 to var256 /INDEX = ivar(vars) /KEEP = id /NULL = KEEP. AGGREGATE /OUTFILE=* MODE=ADDVARIABLES /BREAK=id /vars_min = MIN(vars). select if vars eq vars_min . execute . SORT CASES BY id . CASESTOVARS /ID = id /GROUPBY = VARIABLE . Hillel Vardi BGU Hui Zhao wrote: > Hello, > I need to create a string variable that shows name of the variable > which has the lowest value. I have 256 numeric variables: var1 to > var256. The value of the new string variable should look like "var1" > or "var33". I am a newbie to macros and failed hundred times of trial. > Any suggestions would be appreciated. Thanks! > > > Hui > > ===================== > 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 |
|
In reply to this post by Art Kendall
A release 17 Python solution. This defines a Python function called findMin. The function takes two parameters: the starting and ending variables in file order. This includes all numeric variables between the start and end variables but excludes strings. It adds the string variable minvar to the end of the dataset.
dataset close all. new file. output close all. data list list /var1 var2 var3 var4 var5. begin data 1,2,3,4,5 2,1,3,4,5 3,2,1,4,5 ,2,,4,5 ,,,,, 1,,,,5 3,2,4,1,4 end data. string minvar (a7). begin program. from __future__ import with_statement import spss with spss.DataStep(): def findMin(startvar, endvar): with spss.DataStep(): dataset = spss.Dataset() variableList = dataset.varlist start=variableList[startvar].index end=variableList[endvar].index+1 caseList=dataset.cases for i in range (len(caseList)): cl=caseList[i,start:end] try: m=cl.index(min([v for v in cl if not v is None])) caseList[i,-1]=variableList[m+start].name except: caseList[i,-1]='missing' ###example### findMin('var2', 'var4') end program. -----Original Message----- From: Oliver, Richard Sent: Thursday, September 25, 2008 2:39 PM To: '[hidden email]'; [hidden email] Subject: RE: Re: Find variable with the lowest value for each case Maybe I'm missing something, but in the case where the variables are already conveniently named with the same prefix and a consecutive numeric suffix: string minvar (a6). compute minvar="var1". compute #min=var1. vector v=var1 to var256. loop #i=2 to 256. do if v(#i) < #min. compute #min=v(#i). compute minvar=concat("var", ltrim(string(#i, f3))). end if. end loop. This should find the FIRST variable with the lowest value, but won't tell you if there are others with that same lowest value. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Art Kendall Sent: Thursday, September 25, 2008 11:27 AM To: [hidden email] Subject: Re: Find variable with the lowest value for each case copy, paste, and run this example syntax into a newly opened instance of SPSS. if it does what you want adapt it to your data. if not please describe in more detail what you want to do. * make up some data. new file. set seed = 20080925. input program. vector var (256,f1). loop id = 1 to 50. loop #k = 1 to 256. compute var(#k) = rnd(rv.uniform(0,100.5)). end loop. end case. end loop. end file. end input program. formats id (f3) var1 to var256 (f2). *adapt this to your data. compute minimum = min.1(var1 to var256). *run this syntax once to get from the frequencies *the largest number of variables that are *at the minimum value for the case. string hitvar1 hitvar2 hitvar3 (A6). compute hits = 0. do repeat v= var1 to var256/iv=1 to 256. do if v = minimum. do if hits eq 0. compute hits=hits+1. compute hitvar1 = concat('var',string(iv,f3)). else if hits=1. compute hits=hits+1. compute hitvar2 = concat('var',string(iv,f3)). else if hits=2. compute hits=hits+1. compute hitvar3 = concat('var',string(iv,f3)). else. compute hits=hits+1. end if. end if. end repeat. frequencies vars = minimum hits hitvar1 to hitvar3. Art Kendall Social Research Consultants Hui Zhao wrote: > Hello, > I need to create a string variable that shows name of the variable > which has the lowest value. I have 256 numeric variables: var1 to > var256. The value of the new string variable should look like "var1" > or "var33". I am a newbie to macros and failed hundred times of trial. > Any suggestions would be appreciated. Thanks! > > > Hui > > ===================== > 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 |
| Free forum by Nabble | Edit this page |
