+ Reply to Thread
Results 1 to 8 of 8

Thread: Coding Help

  1. #1
    Points: 1,081, Level: 17
    Level completed: 81%, Points required for next Level: 19

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coding Help



    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!

  2. #2
    RotParaTon
    Points: 46,248, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,080
    Thanks
    211
    Thanked 1,608 Times in 1,378 Posts

    Re: Coding Help

    It would help if you told us what programming language you're working with.

  3. #3
    TS Contributor
    Points: 6,808, Level: 54
    Level completed: 29%, Points required for next Level: 142

    Posts
    775
    Thanks
    0
    Thanked 70 Times in 69 Posts

    Re: Coding Help

    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.

  4. #4
    RotParaTon
    Points: 46,248, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,080
    Thanks
    211
    Thanked 1,608 Times in 1,378 Posts

    Re: Coding Help

    Quote Originally Posted by Mean Joe View Post
    I assume by your name that you are coding in SAS.
    Ha. Good call. I didn't even process the username. But it still would be a good idea for them to give that kind of information. I'll move the thread and leave a redirect.

  5. #5
    Points: 1,259, Level: 19
    Level completed: 59%, Points required for next Level: 41

    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Coding Help

    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;

  6. #6
    Ninja say what!?!
    Points: 8,297, Level: 61
    Level completed: 49%, Points required for next Level: 153
    Link's Avatar
    Posts
    1,165
    Thanks
    37
    Thanked 82 Times in 75 Posts

    Re: Coding Help

    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:
    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;
    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.

  7. #7
    RotParaTon
    Points: 46,248, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,080
    Thanks
    211
    Thanked 1,608 Times in 1,378 Posts

    Re: Coding Help

    Quote Originally Posted by Link View Post
    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.
    I have the opposite problem. R's syntax and style makes sense to me and I love it (if you couldn't tell...) but doing any sort of data manipulation in SAS seems foreign to me and the syntax just feels so outdated...

  8. #8
    Ninja say what!?!
    Points: 8,297, Level: 61
    Level completed: 49%, Points required for next Level: 153
    Link's Avatar
    Posts
    1,165
    Thanks
    37
    Thanked 82 Times in 75 Posts

    Re: Coding Help


    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.

+ Reply to Thread

Similar Threads

  1. R coding tutorial
    By satimis in forum R
    Replies: 8
    Last Post: 11-30-2010, 09:46 PM
  2. R coding Convetions
    By Tart in forum R
    Replies: 1
    Last Post: 05-27-2010, 06:00 PM
  3. help with contrast coding
    By misc763 in forum R
    Replies: 4
    Last Post: 11-30-2009, 09:26 AM
  4. Dummy Coding
    By Statshelp in forum Statistics
    Replies: 0
    Last Post: 04-19-2009, 11:31 AM
  5. SAS coding help
    By lulumohca in forum SAS
    Replies: 1
    Last Post: 06-19-2008, 12:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats