Return variable name as value in new variable

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

Return variable name as value in new variable

Jon Bernard-2
Hello SPSSers,

I have a correlation matrix of 105 variables.  The values in the matrix are
all positive, and the diagonals are missing.  Here are some sample data:

DATA LIST LIST /case_lbl (A4) var1 to var5 .
BEGIN DATA
"var1" . .09 .15 .10 .04
"var2" .09 . .01 .04 .17
"var3" .15 .01 . .06 .01
"var4" .10 .04 .06 . .05
"var5" .04 .17 .01 .05
END DATA .

From these data, I am trying to compute 2 new variables: rMax and varMax.
rMax is the maximum row value among the values in var1 to var5, and this is
easy to compute with "COMPUTE rMax=MAX(var1 to var5)".  varMax is
(hopefully) the variable name of the maximum row value, and this I cannot
figure out how to compute.  The resulting data from a working solution would
look like the sample data below:

DATA LIST LIST /case_lbl (A4) var1 to var5 rMax * varMax (A4) .
BEGIN DATA
"var1" . .09 .15 .10 .04 .15 "var3"
"var2" .09 . .01 .04 .17 .17 "var5"
"var3" .15 .01 . .06 .01 .15 "var1"
"var4" .10 .04 .06 . .05 .10 "var1"
"var5" .04 .17 .01 .05 . .17 "var2"
END DATA .

Can anyone help with a reference or solution?  I should note here that while
I have some experience with basic syntax, I have none with macros, scripts,
or Python.

Very many thanks in advance for your advice.  Please let me know if more
information is necessary to propose a solution.

-Jon
Reply | Threaded
Open this post in threaded view
|

Re: Return variable name as value in new variable

Marta Garcia-Granero
Jon Bernard escribió:

> I have a correlation matrix of 105 variables.  The values in the
> matrix are
> all positive, and the diagonals are missing.  Here are some sample data:
>
> DATA LIST LIST /case_lbl (A4) var1 to var5 .
> BEGIN DATA
> "var1" . .09 .15 .10 .04
> "var2" .09 . .01 .04 .17
> "var3" .15 .01 . .06 .01
> "var4" .10 .04 .06 . .05
> "var5" .04 .17 .01 .05
> END DATA .
>> From these data, I am trying to compute 2 new variables: rMax and
>> varMax.
> rMax is the maximum row value among the values in var1 to var5, and
> this is
> easy to compute with "COMPUTE rMax=MAX(var1 to var5)".  varMax is
> (hopefully) the variable name of the maximum row value, and this I cannot
> figure out how to compute.  The resulting data from a working solution
> would
> look like the sample data below:
>
> DATA LIST LIST /case_lbl (A4) var1 to var5 rMax * varMax (A4) .
> BEGIN DATA
> "var1" . .09 .15 .10 .04 .15 "var3"
> "var2" .09 . .01 .04 .17 .17 "var5"
> "var3" .15 .01 . .06 .01 .15 "var1"
> "var4" .10 .04 .06 . .05 .10 "var1"
> "var5" .04 .17 .01 .05 . .17 "var2"
> END DATA .
>
> Can anyone help with a reference or solution?  I should note here that
> while
> I have some experience with basic syntax, I have none with macros,
> scripts,
> or Python.

Have you tried the ol' MATRIX command ? (see "spssbase.pdf" for more
details):

* Your sample dataset *.
DATA LIST LIST /case_lbl (A4) var1 to var5 .
BEGIN DATA
"var1" . .09 .15 .10 .04
"var2" .09 . .01 .04 .17
"var3" .15 .01 . .06 .01
"var4" .10 .04 .06 . .05
"var5" .04 .17 .01 .05
END DATA .
COMPUTE rmax=MAX(var1 TO var5).

MATRIX.
* Get corr data (important: with variable names, see /NAMES=vnames) *.
GET data /VAR=var1 TO var5
         /NAMES=vnames
         /MISSING=ACCEPT /SYSMIS=-1.
COMPUTE k=NROW(data).
COMPUTE pos=MAKE(k,k,0).
* Get position of max row value *.
LOOP i=1 TO k.
- COMPUTE pos(i,:)=GRADE(data(i,:)).
END LOOP.
PRINT pos
 /FORMAT=F8.
COMPUTE varname=MAKE(k,1,"var").
* Get variable name at max value position at each row *.
LOOP i=1 TO k.
- LOOP j=1 TO k.
-  DO IF pos(i,j) EQ k.
-   COMPUTE varname(i)=vnames(j).
-  END IF.
- END LOOP.
END LOOP.
PRINT varname
 /FORMAT=A8.
* Export data *.
SAVE {varname} /OUTFILE='C:\Temp\VariableNames.sav'
/VARIABLES=varmax/STRINGS=varmax.
END MATRIX.

* Add variable to active file *.
MATCH FILES /FILE=*
 /FILE='C:\Temp\VariableNames.sav'.
LIST.

As you can see, no MACRO, script nor Python is needed...

Best regards,
Marta Garcia-Granero
Reply | Threaded
Open this post in threaded view
|

Re: Return variable name as value in new variable

Marta Garcia-Granero
In reply to this post by Jon Bernard-2
Hi Jon

Very important!

I forgot a detail: since your complete file has 105 correlations, you
need to add the following line before the MATRIX command:

SET MXLOOPS=105.

MXLOOPS must always be at least as big as the number of variables
involved in the MATRIX loops

Sorry I forgot.
Marta

> I have a correlation matrix of 105 variables.  The values in the
> matrix are
> all positive, and the diagonals are missing.  Here are some sample data:
>
> DATA LIST LIST /case_lbl (A4) var1 to var5 .
> BEGIN DATA
> "var1" . .09 .15 .10 .04
> "var2" .09 . .01 .04 .17
> "var3" .15 .01 . .06 .01
> "var4" .10 .04 .06 . .05
> "var5" .04 .17 .01 .05
> END DATA .
>
Reply | Threaded
Open this post in threaded view
|

Re: Return variable name as value in new variable

hillel vardi
In reply to this post by Jon Bernard-2
Shalom

here is a simple way of doing it if your variable list is of the form
var1 to var100 .

DATA LIST LIST /case_lbl (A4) var1 to var5 .
BEGIN DATA
"var1" . .09 .15 .10 .04
"var2" .09 . .01 .04 .17
"var3" .15 .01 . .06 .01
"var4" .10 .04 .06 . .05
"var5" .04 .17 .01 .05
END DATA .
compute         maxvarval=0 .
do repeat        var=var1 to var5/
                     num=1 to 5.
do if                var gt maxvarval .
compute          maxvarval=var .
compute          maxvarpos=num.
end if .
end repeat.
string              maxvarname(a6).
compute         maxvarname=concat('var',string(maxvarpos,f1)) .
list                  case_lbl  var1 to var5 maxvarval maxvarname .

If your variable names are not consecutive  you can copy the variables
name from the variable view and pest them into the do repeat command .

Hillel Vardi



Jon Bernard wrote:

> Hello SPSSers,
>
> I have a correlation matrix of 105 variables.  The values in the
> matrix are
> all positive, and the diagonals are missing.  Here are some sample data:
>
> DATA LIST LIST /case_lbl (A4) var1 to var5 .
> BEGIN DATA
> "var1" . .09 .15 .10 .04
> "var2" .09 . .01 .04 .17
> "var3" .15 .01 . .06 .01
> "var4" .10 .04 .06 . .05
> "var5" .04 .17 .01 .05
> END DATA .
>
>> From these data, I am trying to compute 2 new variables: rMax and
>> varMax.
> rMax is the maximum row value among the values in var1 to var5, and
> this is
> easy to compute with "COMPUTE rMax=MAX(var1 to var5)".  varMax is
> (hopefully) the variable name of the maximum row value, and this I cannot
> figure out how to compute.  The resulting data from a working solution
> would
> look like the sample data below:
>
> DATA LIST LIST /case_lbl (A4) var1 to var5 rMax * varMax (A4) .
> BEGIN DATA
> "var1" . .09 .15 .10 .04 .15 "var3"
> "var2" .09 . .01 .04 .17 .17 "var5"
> "var3" .15 .01 . .06 .01 .15 "var1"
> "var4" .10 .04 .06 . .05 .10 "var1"
> "var5" .04 .17 .01 .05 . .17 "var2"
> END DATA .
>
> Can anyone help with a reference or solution?  I should note here that
> while
> I have some experience with basic syntax, I have none with macros,
> scripts,
> or Python.
>
> Very many thanks in advance for your advice.  Please let me know if more
> information is necessary to propose a solution.
>
> -Jon
>