My SAS Tutorials

Thursday 7 March 2013

SAS INTERVIEWS CONCEPTUAL QUESTIONS III

We have a SAS data set:


SAS Data Set One

EID      Week          Attendance
101         1                    5
101         2                    4
101         3                    6
101         4                    3
102         1                    1
102         2                    3
103         3                    6



We need to create A SAS data:

EID  week1 week2 week3 week4    Present days
101      5          4        6         3             18
102      1           3       .          .              4
103      .            .        6        .               6


Write a program to create the desired data set.

Now prepare your Base SAS and Advance SAS Certification Exams with Online Mock tests:  http://exam.sankhyana.com



For training related info kindly mail us at info@sankhyana.com
www.sankhyana.com

5 comments:

  1. Hi,

    The output given is not right, there are three eid 101,102 and 103 but the displayed eid are only two, 103 is missing and due to this the present days values are also not right. As per the original dataset, the total attendance for 101 is 18,102 is 4 and 103 is 6.

    Please check the code i have written, this gives the correct output

    data have;
    input EID Week Attendance;
    cards;
    101 1 5
    101 2 4
    101 3 6
    101 4 3
    102 1 1
    102 2 3
    103 3 6
    ;
    run;

    proc sort data=have;
    by eid;
    run;

    data want;
    set have;
    by eid;
    retain week1-week4;
    array weeks(*) week1-week4;
    if first.eid then
    do i = 1 to 4;
    weeks(i)=.;
    end;
    weeks(week)=Attendance;
    present_days=sum(of weeks(*));
    if last.eid then output want;
    drop i Attendance week;
    run;

    Thanks,
    Jagadish

    ReplyDelete
    Replies
    1. Hello jag,

      Thanks a lot for replying. I have updated the data. you can also use proc transpose. you are really helping me in making my blog better. I am really thankful to you.

      Thanks & Regards,

      Rohit Kumar

      Delete
  2. proc transpose data=have out= two
    prefix= weeks ;
    by eid;
    id week;
    run;
    proc print data= two;
    run;
    hi sir
    the below code will works

    data therr;
    set two;
    presen= sum(of weeks:);
    run;
    proc print;
    run;

    thanks & regards
    Vamshi

    ReplyDelete
  3. same we can do by using proc freq as well
    proc freq data=have;
    table eid*week/nopercent nocol norow;
    weight Attendance;
    run;

    Regards,
    Damu

    ReplyDelete
  4. data one;
    input EID Week Attendance;
    datalines;
    101 1 5
    101 2 4
    101 3 6
    101 4 3
    102 1 1
    102 2 3
    103 3 6
    ;
    run;

    proc sort data=one;
    by eid attendance;

    proc transpose data=one out=trns prefix=col;
    by eid ;
    id week;
    var attendance;
    run;

    data trns;
    retain eid col1 col2 col3 col4 present_days;
    set trns;
    present_days=sum(col1,col2,col3,col4);
    drop _:;
    run;


    ReplyDelete