Computing new variable

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

Computing new variable

Jean Hanson

Dear Listers,

I am trying to compute a new variable from a list of variables. Each of these variables has a value of 1 or missing.The order is important.The first time a 1 is encountered, I want this included in the new variable, with ascending values. I have tried a variety of do, if statements - one follows. Any help would be appreciated.

 

COMPUTE UIV_P=0.

DO IF (OCC_P=1) UIV_P=1 .

ELSE IF (C1_P=1) UIV_P=2.

ELSE IF (C2_P=1) UIV_P=3.

ELSE IF (C3_P=1) UIV_P=4.

ELSE IF (C4_P=1) UIV_P=5.

ELSE IF (C5_P=1) UIV_P=6.

ELSE IF (C6_P=1) UIV_P=7.

ELSE IF (C7_P=1) UIV_P=8.

END IF.

 

In advance, thanks.

 

Jean Hanson

 

Reply | Threaded
Open this post in threaded view
|

Re: Computing new variable

Gyorgy Bea
Hi Jean,

You can group the variables in a vector and use a loop structure. This assumes you have your variables in the order you had written (OCC_P, C1_P, C2_P ... C7_P) with no other variables between them.

vector v=OCC_P to C7_P.
compute UIV_P=0.
loop #i=1 to 8.
  do if v(#i)=1.
     compute UIV_P=#i.
     BREAK.
  end if.
end loop.

In this case the syntax is not too much shorter than the one with "do if" statements, but it can make difference when you have tens or hundreds of variables.

Best regards,
Beata


From: Jean Hanson <[hidden email]>
To: [hidden email]
Sent: Tuesday, April 7, 2009 1:58:58 AM
Subject: Computing new variable

Dear Listers,

I am trying to compute a new variable from a list of variables. Each of these variables has a value of 1 or missing.The order is important.The first time a 1 is encountered, I want this included in the new variable, with ascending values. I have tried a variety of do, if statements - one follows. Any help would be appreciated.

 

COMPUTE UIV_P=0.

DO IF (OCC_P=1) UIV_P=1 .

ELSE IF (C1_P=1) UIV_P=2.

ELSE IF (C2_P=1) UIV_P=3.

ELSE IF (C3_P=1) UIV_P=4.

ELSE IF (C4_P=1) UIV_P=5.

ELSE IF (C5_P=1) UIV_P=6.

ELSE IF (C6_P=1) UIV_P=7.

ELSE IF (C7_P=1) UIV_P=8.

END IF.

 

In advance, thanks.

 

Jean Hanson