It would help if you told us what programming language you're working with.
I'm sorry if this isn't the correct place to post this coding question....
I have a dataset with a number of reaction codes (reactioncode1, reactioncode2, reactioncode3....) and I want to classify the reactions by mild moderate and severe.
I know there is a faster/cleaner way of doing this then the method I have set out on:
if reactioncode1 = '1' or reactioncode1 = '2' or reactioncode1 = '3' or reactioncode2 = '1' or reactioncode2 = '2' or reactioncode2 = '3'......then class = 'severe'
else if reactioncode1 = '4' or reactioncode1 = '5' or reactioncode1 = '6'......
can anybody help me with this (or else I will be typing this code all day long - there are a lot of reactioncodes and a lot of possible events).
there is no way of doing:
if reactioncode1 = ('1' or '2' or '3') or reactioncode2 = ('1' or '2' or '3') then class = 'severe'
else if.....
is there?
Thank you in advance!
It would help if you told us what programming language you're working with.
I assume by your name that you are coding in SAS.
#1 - Straightforward, requires a bit less typing:
if ('1' <= reactioncode1 <= '3') or ('1' <= reactioncode2 <= '3') or ('1' <= reactioncode3 <= '3') then class='severe';
else if ('4' <= reactioncode1 <= '6') or ...;
#2 - assign the character-type variables into a number-type variable, and then use the MIN function
reactioncode1n=reactioncode1*1; *reactioncode1N = reactioncode1 in numeric form;
reactioncode2n=reactioncode2*1;
reactioncode3n=reactioncode3*1;
if 1 <= min(reactioncode1n, reactioncode2n, reactioncode3n) <= 3 then class='severe';
else if 4 <= min(reactioncode1n, reactioncode2n, reactioncode3n) <= 6 then class='moderate';
else if 7 <= ...
note: you'll get a warning from SAS from this code (reactioncode1n=reactioncode1*1) because you are performing a mathematical operation on a character-type variable, but the program will run. The clean way to do it is (reactioncode1n=input(reactioncode1,8.)), but the method above is pretty commonly used by SAS users.
other way . It's possible if you use or statement and not and.
class='' ;
array x {*} reactioncode1-reactioncode10 ; * for example n=10 ;
do i=1 to dim(x) ;
if x[i] in ('1','2','3') then class='severe';
else class^='' and x[i] in ('4','7','10') then class='test';
end;
hehehe....thank you very much for posting this. With the frustration I go through learning the "style" and syntax of R, it feels good coming back to the language I'm so familiar with.
What you're looking for is:
Please note that this is very similar to ariari's code. That is because I simply copied and pasted it over with my own style. All credit should go to him.Code:data "datasetname"; class=""; array reactions reactioncode1-reactioncode10; * for example n=10; do i=1 to dim(reactions); if x[i] in ('1','2','3') then class='severe'; else if (class="") and (x[i] in ('4','5','6')) then class='moderate'; else if (class="") and (x[i] in ('7','8','9')) then class='mild'; etc... end; run;
lol! I guess it's just taste really. That and I was already good at SAS before I was even introduced to R.
Getting somewhat more comfortable with R, I do see what you're talking about. I tend to see that companies tend to go with SAS (likely because it handles large datasets so well). R does have its own strengths though (among them the fact that its free). What really gets tricky is going back and forth between the two languages! Both handles data differently, and I find myself approaching SAS programming with R mentality and vice versa.
|
|