Saturday, April 11, 2020

SAS Practice Examples


Create a Student table with studentnum, quarter, math, science, english marks?

data Student;
 input studentnum $ quarter math science english;
 datalines;
 1001 1 70 80 76
 1001 2 80 80 78
 1001 3 90 85 80
 ;
run;

Add 4th quarter marks as a new record and create a new Student_1 table?

data Student_1;
 set Student end=eof;
 output;
 if eof then do;
   studentnum = '1001';
   quarter = 4;
   math = 88;
   science = 90;
   english = 88;
   output;
 end;
run;

Add the total for each quarter?

data Student_marks_total;
 set Student_1;
 total = math + science + english;
run;

Add the average for each column including the total as a new row in the bottom?

data Student_marks_avg;
 
 set Student_marks_total end=eof;
 drop sum_math sum_science sum_english sum_total;
 retain sum_math sum_science sum_english sum_total 0;
 
 sum_math = sum_math + math;
 sum_science = sum_science + science;
 sum_english = sum_english + english;
 sum_total = sum_total + total;
 
 output;
 
 if eof then do;
  studentnum = 'avg';
  quarter = .;
  math = sum_math / 4;
  science = sum_science / 4;
  english = sum_english / 4;
  total = sum_total / 4;
  output;
 end;
run; 

Sort the Student_1 table by reversing the quarter?

proc sort data=student_1 out=student_1_sort_by_quarter;
 by descending quarter;
run;

Combine Sales, Delivery employee datasets as Marketing dataset?

data Sales;
 input empno empname $;
 datalines;
 1001 Daniel
 1002 Peter
 ;
run;

data Delivery;
 input empno empname $;
 datalines;
 1003 Simon
 1004 Chris
 ;
run;

data Marketing;
 set Sales Delivery;
run;

Merge a Student's arts and groups marks into a single dataset?

data student_arts;
 input studentnum english spanish latin;
 datalines;
 1001 80 70 78
 ;
run;

data student_groups;
 input studentnum math science history;
 datalines;
 1001 88 78 89
 ;
run;

data student_arts_and_groups;
 merge student_arts student_groups;
 by studentnum;
run;

Is it possible to create a table in a simple way with couple of records and couple of columns?

data blah;
 firstname = 'Sean'; 
 lastname = 'Connery';
run;

Or with two records

data blah;
 firstname = 'Sean'; 
 lastname = 'Connery';
 output;
 firstname = 'Matt';
 lastname = 'Damon';
 output;
run;

Calculate average and grade the student based on this grading guidelines?
failed: below 35
ordinary: > 35 and <= 50
second: > 50 and <= 60
first: > 60 and <= 70
distinction: > 70

data Student;
 input studentnum $ math science english;
 datalines;
 1001 40 50 46
 1002 60 60 68
 1003 90 85 80
 1004 10 20 23
 ;
run;

data Student_grading;
 set Student;
 
 average = (math + science + english) / 3;
 average = Round(average, 0.01);
 
 length grading $ 12;
 
 if average > 35 and average <= 50 then grading = 'ordinary';
 else if average > 50 and average <= 60 then grading = 'second';
 else if average > 60 and average <= 70 then grading = 'first';
 else if average > 70 then grading = 'distinction';
 else grading = 'failed';
 
run;

Based on Student marks, decide the grading and add some comments?
(NOTE: more than one action in if-then statement, so you have to use do)

data student;
 input studentnum marks;
 datalines;
 1001 49
 1002 89
 1003 34
 1004 89
 ;
run;

data student_report;
 set student;
 if marks > 50 then do;
  result = 'pass';
  comments = 'eligible to go to next level';
 end;
 else do;
  result = 'fail';
  comments = 'not eligible to next level';
 end;
run;

How to add a column with table name?

data report_card;
 set student_grading indsname=name;
 tablename=scan(name,2);
run;

Create a temp dataset out of Student dataset using SQL?

proc sql;
 create table student_temp as
 select * from student;
quit;

Add numbers 1 to 5 and give the sum?

data sum_1_to_5;
 sum = 0;
 do i = 1 to 5;
 sum = sum + i;
 end;
 drop i;
run;

Nik has 10 dollars. Vik has 16 dollars. Keep giving Nik a dollar per day till they both have equal money?
(Note: Use Do while loop)

data level_the_brothers;
 nik_money = 10;
 vik_money = 16;
 
 do while (nik_money < vik_money);
  nik_money = nik_money + 1;
 end;
 
 final_nik_money = nik_money;
 final_vik_money = vik_money;
 
 drop nik_money vik_money;
run;

Nik has 10 dollars. Vik has 16 dollars. Keep giving Nik a dollar per day till they both have equal money?
(Note: Use Do until loop)

data level_the_brothers;
 nik_money = 10;
 vik_money = 16;
 
 do until (nik_money = vik_money);
  nik_money = nik_money + 1;
 end;
 
 final_nik_money = nik_money;
 final_vik_money = vik_money;
 
 drop nik_money vik_money;
run;

Find the total, average marks of students if all 6 subjects marks are provided for each student?

data array_example;
 input studentnum s1 s2 s3 s4 s5 s6;
 array s(6) s1-s6; /* Hint - array element names match column names */
 marks_total = sum(of s(*));
 marks_avg = round(mean(of s(*)), 0.01);
 datalines;
 1001 78 56 45 34 75 65
 1002 56 67 78 89 45 34
 ; 
run;

No comments:

Post a Comment