Solved
Contributor
Posts: 28

Compute a new variable from multiple variables

Dear anyone :-)

I want to compute a new variable (q_new) from the variables q1 - q6. The possible values of q1 - q6 is the letters a - j, but none of the letters can appear more than onceCompute variables.

If the letter 'a' is present in any of the variables q1-q6, then q_new=1, else q_new=0. Any suggestions on how to do this the smart way?

Sincerely

Anders Lødrup

 ID q1 q2 q3 q4 q5 q6 q_new 1001 a c e 1002 a b c d h 1003 a c d f g j

Accepted Solutions
Solution
‎09-06-2012 07:58 AM
Posts: 4,736

Re: Compute a new variable from multiple variables

One out of many possibilities:

array Qarr {*} q1-q6;

q_new=  ( findc(cats(of Qarr(*)),'a','i') >0 );

All Replies
Solution
‎09-06-2012 07:58 AM
Posts: 4,736

Re: Compute a new variable from multiple variables

One out of many possibilities:

array Qarr {*} q1-q6;

q_new=  ( findc(cats(of Qarr(*)),'a','i') >0 );

Contributor
Posts: 28

Re: Compute a new variable from multiple variables

Hi Patric

Thank you for the response. Unfortunately, this does not do the trick. It sort of erases most of the data and comes up with this table:

 q1 q2 q3 q4 q5 q6 q_new 1 . . . . . . 1

Anders

Super Contributor
Posts: 1,636

Re: Compute a new variable from multiple variables

Hi,

Patrick's code works for me. It didn't work for you maybe you use different version of SAS.  you can try another solution I provided.

data have;
infile cards missover;
input (q1-q6) (\$);
cards;
a c e
a b c d e l
b d d e l c
;
data want;
set have;
array Qarr {*} q1-q6;
q_new=  ( findc(cats(of Qarr(*)),'a','i') >0 );
run;

/*or */
data want2(drop=n);
set have;
n=0;
array Qarr {*} q1-q6;
do _n_=1 to dim(qarr);
n+sum(lowcase(qarr(_n_))='a'); end;
if n>0 then q_new=1;
else q_new=0;
proc print;run;

Super User
Posts: 6,781

Re: Compute a new variable from multiple variables

Anders,

You have to put Patrick's solution in context.  Your DATA step that applies his solution should have a SET statement.

Contributor
Posts: 28

Re: Compute a new variable from multiple variables

Thank you Astounding

I'm a twat that I didn't see it. Now it works :-)

A

Valued Guide
Posts: 765

Re: Compute a new variable from multiple variables

hi ... how about the IN operator (shown by KSharp a few weeks ago ... used it to de-duplicate an array) ...

data have;

infile cards missover;

input (q1-q6) (\$);

cards;

a c e

a b c d e l

b d d e l c

;

data want;

set have;

array q(6);

q_new = 'a' in q;

run;

q1    q2    q3    q4    q5    q6    q_new

a     c     e                         1

a     b     c     d     e     l       1

b     d     d     e     l     c       0

ps  for the solutions with FIND or INDEX, you could also try ^^ (also recently discussed.here) ..

q_new = ^^index(catt(of q,'a');

Contributor
Posts: 28

Super

Thanks a lot :-)

cheers Anders

Contributor
Posts: 28

Super

Thanks a lot :-)

cheers Anders

Contributor
Posts: 28

Super

Thanks a lot :-)

cheers Anders

Contributor
Posts: 26

Re: Compute a new variable from multiple variables

Try  using the index. This demo is 'case sensitive'.

DATA t;

LENGTH q1-q6 \$ 1;

Input id (q1-q6) (\$2.);

q_new = (Index(catt(OFq,'a')>0);

CARDS;

1001 a c e

1002 b c d h a

1003 K c d f g j

1004 A c d f g j

;;;;

Super User
Posts: 23,752

Re: Compute a new variable from multiple variables

Depending on your SAS version you can try the whichc function, but it gives the position, not a 1 or 0.

whichc('a', q1,q2,q3,q4,q5,q6)

You can easily add a check if that is greater than 0 to change it to a 1.

If the new variable wasn't called q1_new you could also go whichc('a', of q which would check any that started with a q.

Valued Guide
Posts: 765

Re: Compute a new variable from multiple variables

hi Reeza ... you could also shorten the list and add the double NOT to get a 0 or 1...

q_new = ^^whichc('a', of q1-q6);

Super Contributor
Posts: 1,636

Re: Compute a new variable from multiple variables

or:

q_new=whichc('a', of q1-q6)>0;

Contributor
Posts: 28

Re: Compute a new variable from multiple variables

Super

Thanks a lot :-)

cheers Anders

🔒 This topic is solved and locked.