Help using Base SAS procedures

IF THEN STATEMENT

Reply
Regular Contributor
Posts: 161

IF THEN STATEMENT

Hello,

I am using IF Then statement to label my binary data by their combination. I have 3 binary variables which means 2**3=7 combination:
this is my data:
A B C
1 1 1
0 0 1
and so on

so, the first row will be combination 8, 2nd combination 1,....
Right now I am using IF Then statement which means 8 lines of "if then" how can I avoid this and use a shorter code?
thank you
Regular Contributor
Posts: 165

Re: IF THEN STATEMENT

You could do something like...

comb=input(catx(put(A,1.),put(B,1.),put(C,1.)),binary3.);

Good luck!
Regular Contributor
Posts: 161

Re: IF THEN STATEMENT

Thanks Rick,
it worked I replaced my code with yours (had to switch 1st statemment with the 2nd). Do you mind explaining what your code did.
Thanks again

this was my code(longSmiley Happy):
IF SKILLS1=0 and SKILLS2=0 AND SKILLS3=1 THEN COMBINATION=1;
if SKILLS1=0 and SKILLS2=1 AND SKILLS3=0 THEN COMBINATION=2;
IF SKILLS1=0 and SKILLS2=1 AND SKILLS3=1 THEN COMBINATION=3;
IF SKILLS1=1 and SKILLS2=0 AND SKILLS3=0 THEN COMBINATION=4;
IF SKILLS1=1 and SKILLS2=0 AND SKILLS3=1 THEN COMBINATION=5;
IF SKILLS1=1 and SKILLS2=1 AND SKILLS3=0 THEN COMBINATION=6;
IF SKILLS1=1 and SKILLS2=1 AND SKILLS3=1 THEN COMBINATION=7;
Regular Contributor
Posts: 165

Re: IF THEN STATEMENT

It basically concatenated the 0's and 1's into a string ('111', '101', '001', etc) and then that string was converted to a number based on the binary informat.

You could also have calculated the binary number from the individual variables.

comb=A*2**2+B*2**1+C*2**0;
Regular Contributor
Posts: 161

Re: IF THEN STATEMENT

thank you so much
R.A.G.
Super User
Posts: 9,681

Re: IF THEN STATEMENT

It looks like you are doing convert binary into dec



[pre]
data _null_;
retain combination -1;
do skills1=0,1;
do skills2=0,1;
do skills3=0,1;
combination+1;
put (_all_) (Smiley Happy;
end;
end;
end;
run;
[/pre]





Ksharp
Ask a Question
Discussion stats
  • 5 replies
  • 163 views
  • 0 likes
  • 3 in conversation