Thursday, 24 July 2014

SAS Basics Part-2

In this we will learn about how modify and label variables.


1. Import the following data into a SAS dataset called students and create labels for variable names.
Score1 – Maths, Score2 – English, Score3 – Physics, Score4 – Chemistry, Score5 - Biology

Score1 Score2 Score3 Score4 Score5
90 98 98
80 100 98 70 78
20 50 90 30 60

Ans:

Data Students;
input Score1 Score2 Score3 Score4 Score5;
label Score1='Maths'
    Score2='English'
    Score3='Physics'
    Score4='Chemistry'
    Score5='Biology';
    datalines;
    90 98 98 . .
    80 100 98 70 78
    20 50 90 30 60
    ;
    run;
   
    proc print data=students; run;

Output:
                                                                           Students


ObsScore1Score2Score3Score4Score5
1909898..
280100987078
32050903060

2. Rename the variables to Maths, English, Physics, Chemistry, and Biology.


Ans:
proc contents data=Students; run;

output:


Alphabetic List of Variables and Attributes
#VariableTypeLenLabel
1Score1Num8Maths
2Score2Num8English
3Score3Num8Physics
4Score4Num8Chemistry
5Score5Num8Biology
   
    Data Students;
set Students(rename=(score1=maths score2=English score3=physics score4=chemistry score5=biology)) ;
run;
proc contents data=Students; run;

Alphabetic List of Variables and Attributes
#VariableTypeLenLabel
2EnglishNum8English
5biologyNum8Biology
4chemistryNum8Chemistry
1mathsNum8Maths
3physicsNum8Physics

3. What is the difference between these two codes? – 

Data students;
Set students (rename = (Score3 = Physics Score4 = Chemistry Score5 = Biology));
Run;

Data students (rename = (Score3 = Physics Score4 = Chemistry Score5 = Biology));
Set students;  Run;

Ans:

In the first after fetching the variables from Input buffer to PDV variables are renamed,
whereas  in the second code before the writing the data to the output buffer the variable names are renamed

4. Create a new data from dataset students called PCM with scores only for Maths, 
Physics, and Chemistry.
Ans:
Data PCM;
Set Students(Keep= Physics Chemistry Maths);
run;
proc print data=PCM;run;

Output:
                                                                             PCM


Obsmathsphysicschemistry
19098.
2809870
3209030

5. Create a new variable called Science which is addition of scores of Physics, Chemistry & Biology.
Ans:
Data PCM;
set PCM;
Science=Sum(Physics,Chemistry,Maths);
run;
proc print data=PCM;run;

Output:                                                                PCM


ObsmathsphysicschemistryScience
19098.188
2809870248
3209030140

6. What is the difference between these two codes – 
Data students;
Set students (rename = (Score3 = Physics Score4 = Chemistry Score5 = Biology));
Science = Physics + Chemistry + Biology;
Run;

Data students;
Set students (rename = (Score3 = Physics Score4 = Chemistry Score5 = Biology));
Science = Sum (Physics, Chemistry, Biology);
Run;

Ans:
SUM function returns the sum of non-missing arguments whereas “+” 
operator returns a missing value if any of the arguments are missing.


7. Assume that 15 marks are to be added to the scores of Physics, Chemistry & Biology for lab work. 
Write a code to do the same.
Ans:

Proc sql;
Update PCM set Physics=Physics+15,Maths=Maths+15,Chemistry=Chemistry+15;
title 'Updated scores';
Select * from PCM;
quit;

Before adding 15 to the score

  Before Adding 


ObsmathsphysicschemistryScience
19098.188
2809870248
3209030140

                                                                      After adding


MathsPhysicsChemistryScience
105113.188
9511385248
3510545140

No comments:

Post a Comment