My SAS Tutorials

Wednesday 27 February 2013

SAS INTERVIEWS CONCEPTUAL QUESTIONS I

We have a raw data set:


Formatted Input 

Ram     21 500    Friday,January 1,1960
Sita      22  600   Saturday,january 1,1960
Radha 23 700     Sunday,january 1,1960

We have to create a SAS dataset from this raw data set.

output:

Name       Age    Salary     Hire_date
Ram           21       500       01jan1960
Sita            22       600       02jan1960
Radha       23        700       03jan1960

Please write SAS Program to complete this task.

Solution:



data one;
infile 'Filename';
length x $ 10 y $ 10 z $ 10 b $ 15;
input name $ age salary @15 H_D $30.;
keep name age salary Hire_date;
x=scan(H_D,2);
z=scan(H_D,4);
a=strip(x)||strip(z);
b=tranwrd(a,substr(a,1,length(a)-4),substr(a,1,3));
day+1;
k=month(input(b,monyy7.));
Hire_Date=mdy(k,day,z);
format final date9.;
run;
proc print data=one;
run;



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

8 comments:

  1. There is a data issue in the data provided, if you see even if the weekday is
    changing, the date is constant, which is not possible. it should have been
    below

    Ram 21 500 friday,January 1,1960
    Sita 22 600 Saturday,january 2,1960
    Radha 23 700 Sunday,january 3,1960

    if the data is like above and if you want to read it, you can read
    name,age,salary without much problem, however if you want to read the
    date then you need to use the informat. however we do not have any
    informat to read it.

    In these case you need to create the informat and use the created informat for
    reading the data.

    please check the code below which will create the informats


    /*this is the dataset which i am suing to read the data provided*/
    data format;
    fmtname='myformat';
    type='I';
    do label='01jan1960'd to '01jan1961'd;
    start=lowcase(strip(put(label,weekdate.)));
    output;
    end;
    run;

    /*this will create the format from the dataset*/

    proc format cntlin=format fmtlib;
    run;

    /*applying the informat to read the data*/

    data have(keep=name age salary hire_date);
    input name $ age salary date & $100. ;
    date1=tranwrd(lowcase(strip(date)),',',', ');
    hire_date=input(date1,myformat.);
    format hire_date date9.;
    cards;
    Ram 21 500 friday,January 1,1960
    Sita 22 600 Saturday,january 2,1960
    Radha 23 700 Sunday,january 3,1960
    ;
    run;

    proc print;
    run;

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Thanks Jagdish..There is not any data error...Kindly go through the solution provided..

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Hi Rohit,

      Thank you for informing that the data is correct, i wrote the code considering that the data was incorrect and changed the data.

      i checked your code by executing and it did not give the desired output.

      Here is the output that i got from your code


      Obs name age salary final

      1 Ram 21 500 .
      2 Sita 22 600 .
      3 Radha 23 700 .

      there is no final variable created in the dataset but was used in the keep statement. I believe you wanted to use the Hire_date variable, however if you check the hire_date value it is 0 for all the three observation, so when you use the format date9.. it will give the same date 01Jan1960 for all the three observations.

      I have written another code without changing the data this time giving the same output as wished.

      please check the code below

      data one(keep=name age salary hire_date);
      input name $ age salary @15 H_D $30.;
      month=substr(scan(h_d,2),1,3);
      day+1;
      year=scan(h_d,4);
      hire_date=input(put(day,2.)||compress(month)||year,date9.);
      format hire_date date9.;
      cards;
      Ram 21 500 Friday,January 1,1960
      Sita 22 600 Saturday,january 1,1960
      Radha 23 700 Sunday,january 1,1960
      run;

      proc print;
      run;

      here is the output that i got

      Obs name age salary hire_date
      1 Ram 21 500 01JAN1960
      2 Sita 22 600 02JAN1960
      3 Radha 23 700 03JAN1960

      Please let me know if there is any correction required. Will be happy to correct it.

      Thanks,
      Jagadish

      Delete
    5. Hello Jag,

      Thanks a lot. I have updated my program. This Will work.

      Regards
      Rohit Kumar

      Delete
  2. ttp://chennaitraining.in/dotnet-training-in-chennai/
    http://chennaitraining.in/etl-testing-training-in-chennai/
    http://chennaitraining.in/salesforce-admin-training-in-chennai/
    http://chennaitraining.in/salesforce-developer-training-in-chennai/
    http://chennaitraining.in/sap-hana-training-in-chennai/
    http://chennaitraining.in/sap-mm-training-in-chennai/

    ReplyDelete
  3. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    موعد الراتب

    ReplyDelete