Data Transformation

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

Data Transformation

Edward Boadi
Dear List,

I have two variables  Var1 and Var2 .

Var1     Var2
a               15
b               26
a               68
b               42
c               83
d               68

How do I create another variable Var3 such that , Var3 = var2/83  if  var1 = a , and Var3= Var2/68  when Var1 = b.

I am currently using the syntax below.

IF (Var1 = 'a') Var3 = (Var2/83).
IF (Var1 = 'b') Var3 = (Var2/68).
Execute.

 I find it to be inefficient because I want to be able to do the computation without knowing what the correwsponding
 values of  Var1 "C" and "D" are for Var2.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Data Transformation

Marta García-Granero
Hi Edward,

EB> I have two variables  Var1 and Var2 .
EB> Var1     Var2
EB> a               15
EB> b               26
EB> a               68
EB> b               42
EB> c               83
EB> d               68

EB> How do I create another variable Var3 such that , Var3 =
EB> var2/83  if  var1 = a , and Var3= Var2/68  when Var1 = b.

EB> I am currently using the syntax below.

EB> IF (Var1 = 'a') Var3 = (Var2/83).
EB> IF (Var1 = 'b') Var3 = (Var2/68).
EB> Execute.

EB>  I find it to be inefficient because I want to be able to do
EB> the computation without knowing what the correwsponding
EB>  values of  Var1 "C" and "D" are for Var2.

If I understand your question correctly, if var1=a then
var=var2/(value of var2 for var1 = c), and var1=b, then
var3=var2/(value of var2 when var1= d).

Although there might be a simpler way, since nobody answered to your
question, here's my solution, using MATRIX (C:\Temp folder must exist,
create it or replace by another folder):

* Your sample data *.
DATA LIST LIST/ var1(A1) var2(F8).
BEGIN DATA
a 15
b 26
a 68
b 42
c 83
d 68
END DATA.

MATRIX.
GET data  /VAR=var1 var2 /MISSING=OMIT.
LOOP i=1 TO NROW(data).
- DO IF data(i,1) EQ 'c'.
-  COMPUTE aden=data(i,2).
- ELSE IF data(i,1) EQ 'd'.
-  COMPUTE bden=data(i,2).
- END IF.
END LOOP.
COMPUTE var3=MAKE(NROW(data),1,0).
LOOP i=1 TO NROW(data).
- DO IF data(i,1) EQ 'a'.
-  COMPUTE var3(i)=data(i,2)/aden.
- ELSE IF data(i,1) EQ 'b'.
-  COMPUTE var3(i)=data(i,2)/bden.
- END IF.
END LOOP.
COMPUTE vname={'var3'}.
SAVE var3 /OUTFILE='C:\Temp\var3.sav' /NAME=vname.
END MATRIX.

MATCH FILES /FILE=*
 /FILE='C:\temp\var3.sav'.
RECODE var3 (0=SYSMIS)  .
EXECUTE .

HTH

Marta
Reply | Threaded
Open this post in threaded view
|

Re: Data Transformation

Edward Boadi
In reply to this post by Edward Boadi
Issue resolved,
Thanks Marta and Roberts for your syntax.

Regards

Edward


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Marta García-Granero
Sent: Wednesday, June 28, 2006 7:29 AM
To: [hidden email]
Subject: Re: Data Transformation


Hi Edward,

EB> I have two variables  Var1 and Var2 .
EB> Var1     Var2
EB> a               15
EB> b               26
EB> a               68
EB> b               42
EB> c               83
EB> d               68

EB> How do I create another variable Var3 such that , Var3 =
EB> var2/83  if  var1 = a , and Var3= Var2/68  when Var1 = b.

EB> I am currently using the syntax below.

EB> IF (Var1 = 'a') Var3 = (Var2/83).
EB> IF (Var1 = 'b') Var3 = (Var2/68).
EB> Execute.

EB>  I find it to be inefficient because I want to be able to do
EB> the computation without knowing what the correwsponding
EB>  values of  Var1 "C" and "D" are for Var2.

If I understand your question correctly, if var1=a then
var=var2/(value of var2 for var1 = c), and var1=b, then
var3=var2/(value of var2 when var1= d).

Although there might be a simpler way, since nobody answered to your
question, here's my solution, using MATRIX (C:\Temp folder must exist,
create it or replace by another folder):

* Your sample data *.
DATA LIST LIST/ var1(A1) var2(F8).
BEGIN DATA
a 15
b 26
a 68
b 42
c 83
d 68
END DATA.

MATRIX.
GET data  /VAR=var1 var2 /MISSING=OMIT.
LOOP i=1 TO NROW(data).
- DO IF data(i,1) EQ 'c'.
-  COMPUTE aden=data(i,2).
- ELSE IF data(i,1) EQ 'd'.
-  COMPUTE bden=data(i,2).
- END IF.
END LOOP.
COMPUTE var3=MAKE(NROW(data),1,0).
LOOP i=1 TO NROW(data).
- DO IF data(i,1) EQ 'a'.
-  COMPUTE var3(i)=data(i,2)/aden.
- ELSE IF data(i,1) EQ 'b'.
-  COMPUTE var3(i)=data(i,2)/bden.
- END IF.
END LOOP.
COMPUTE vname={'var3'}.
SAVE var3 /OUTFILE='C:\Temp\var3.sav' /NAME=vname.
END MATRIX.

MATCH FILES /FILE=*
 /FILE='C:\temp\var3.sav'.
RECODE var3 (0=SYSMIS)  .
EXECUTE .

HTH

Marta