+ Reply to Thread
Results 1 to 4 of 4

Thread: Help with Arrays and formating data

  1. #1
    Points: 1,087, Level: 17
    Level completed: 87%, Points required for next Level: 13

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Arrays and formating data



    OK, I have tried so many different ways of doing this, I'm so confused. Thank you all for you help in advance.
    I want to re-organize the data so that each observation has one ID, and the different variables go length wise. As opposed to having three observations of the same ID. The data and code i have tried follows (i have tried so many different options, but have gotten nowhere)

    ID DOC DOS DOB PAID
    001 A 110101 741206 230.11
    002 Z 110102 940625 1204.39
    003 D 110102 771123 45.79
    004 D 110124 420410 105.39
    005 Z 110104 100326 9036.99
    006 A 110105 560220 154.23
    006 D 110118 560220 89.44
    007 D 110105 800413 112.49
    008 A 110109 330202 199.65
    008 Z 110103 330202 2349.66
    009 A 110121 080318 200.34
    009 D 110126 080318 40.89
    009 Z 110117 080318 9840.33
    010 Z 110115 640925 908.32
    011 A 110111 520422 269.32
    012 A 110117 720607 103.33
    012 D 110118 720607 99.38
    012 Z 110122 720607 895.49
    013 D 110105 700614 129.94
    014 D 110107 550915 200.38
    015 D 110109 690318 67.94
    016 D 110110 191121 143.29
    017 A 110111 920125 148.61
    017 D 110103 920125 80.13
    017 Z 110106 920125 1567.38
    018 A 110104 660704 225.74
    018 D 110120 660704 12.59
    019 Z 110109 390811 749.33
    020 D 110110 090311 110.22


    DATA oneID;
    SET hw2;
    ARRAY claims[50,3] DOS1-DOS3;
    DO ID = 1 to 50;
    DO DOC = 1 to 3;
    DOS = DOS[ID, DOC];
    OUTPUT;
    END;
    END;
    RUN;

    PROC PRINT DATA=oneID;
    TITLE 'One ID per Subject';
    RUN;

  2. #2
    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: Help with Arrays and formating data

    First off, this may not be what you want. Each record will have 3 DOC + 3 DOS + 3 PAID variables.
    Use the RETAIN, and FIRST./LAST. functions of SAS


    proc sort data=hw2; by ID DOC; run;

    data oneID(drop=numrecs);
    retain numrecs DOC1 DOC2 DOC3 DOS1 DOS2 DOS3 paid1 paid2 paid3;
    set hw2;
    by ID;
    if first.ID then do;
    numrecs=1; DOC1=DOC; DOS1=DOS; paid1=paid;
    DOC2=" "; DOC3=" "; DOS2=" "; DOS3=" "; paid2=.; paid3=.;
    end;
    else numrecs=numrecs+1;
    if numrecs=2 then do;
    DOC2=DOC; DOS2=DOS; paid2=paid;
    end;
    else if numrecs=3 then do;
    DOC3=DOC; DOS3=DOS; paid3=paid;
    end;
    else put "Error: More than 3 records for : " ID= numrecs= ;
    if last.ID then output;
    run;


    That's it. However, if DOC will only be either A or D or Z then you could make a little change that might be better:
    proc sort data=hw2; by ID DOC; run;

    data oneID;
    retain DOS_A DOS_D DOS_Z paid_A paid_D paid_Z;
    set hw2;
    by ID;
    if first.ID then do;
    DOS_A=" "; DOS_D=" "; DOS_Z=" "; paid_A=.; paid_D=.; paid_Z=.;
    end;
    if DOC="A" then do;
    DOS_A=DOS; paid_A=paid;
    end;
    else if DOC="D" then do;
    DOS_D=DOS; paid_D=paid;
    end;
    else if DOC="Z" then do;
    DOS_Z=DOS; paid_Z=paid;
    end;
    else put "Error: DOC not in A/D/Z: " ID= DOC= ;
    if last.ID then output;
    run;

  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: Help with Arrays and formating data

    Looking at how the stuff got posted to the internet, I wanted to make one thing clear:

    Quote Originally Posted by Mean Joe View Post
    if first.ID then do;
    numrecs=1; DOC1=DOC; DOS1=DOS; paid1=paid;
    DOC2=" "; DOC3=" "; DOS2=" "; DOS3=" "; paid2=.; paid3=.;
    end;

    DOS2="______" <- that should be 6 spaces

    When I read it now, it looks like it just says DOS2="_" (1 space)

    If you quote my response though, the 6 spaces show up.

  4. #4
    RotParaTon
    Points: 46,287, 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,083
    Thanks
    211
    Thanked 1,609 Times in 1,379 Posts

    Re: Help with Arrays and formating data


    That's partially why the code tags are so useful.
    Compare the following sentences.

    Hi. There are six spaces after the word "Hi.".

    Code: 
    Hi.      There are six spaces after the word "Hi.".
    Yay for retaining any indentation and/or spacing!

+ Reply to Thread

Similar Threads

  1. Dates, Formating, and Cutting out Characters
    By StataKid12 in forum Stata
    Replies: 4
    Last Post: 03-22-2011, 07:23 AM
  2. questions on sas formating issue.
    By winecoding in forum SAS
    Replies: 1
    Last Post: 03-04-2011, 02:33 PM
  3. Replies: 1
    Last Post: 11-06-2010, 02:22 AM
  4. Replies: 3
    Last Post: 08-04-2010, 05:09 PM
  5. Help in arrays
    By tanz in forum SAS
    Replies: 0
    Last Post: 08-10-2009, 12:11 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