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;
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
| Obs | Score1 | Score2 | Score3 | Score4 | Score5 |
|---|---|---|---|---|---|
| 1 | 90 | 98 | 98 | . | . |
| 2 | 80 | 100 | 98 | 70 | 78 |
| 3 | 20 | 50 | 90 | 30 | 60 |
2. Rename the variables to Maths, English, Physics, Chemistry, and Biology.
Ans:
proc contents data=Students; run;
output:
| Alphabetic List of Variables and Attributes | ||||
|---|---|---|---|---|
| # | Variable | Type | Len | Label |
| 1 | Score1 | Num | 8 | Maths |
| 2 | Score2 | Num | 8 | English |
| 3 | Score3 | Num | 8 | Physics |
| 4 | Score4 | Num | 8 | Chemistry |
| 5 | Score5 | Num | 8 | Biology |
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 | ||||
|---|---|---|---|---|
| # | Variable | Type | Len | Label |
| 2 | English | Num | 8 | English |
| 5 | biology | Num | 8 | Biology |
| 4 | chemistry | Num | 8 | Chemistry |
| 1 | maths | Num | 8 | Maths |
| 3 | physics | Num | 8 | Physics |
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
| Obs | maths | physics | chemistry |
|---|---|---|---|
| 1 | 90 | 98 | . |
| 2 | 80 | 98 | 70 |
| 3 | 20 | 90 | 30 |
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
| Obs | maths | physics | chemistry | Science |
|---|---|---|---|---|
| 1 | 90 | 98 | . | 188 |
| 2 | 80 | 98 | 70 | 248 |
| 3 | 20 | 90 | 30 | 140 |
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
| Obs | maths | physics | chemistry | Science |
|---|---|---|---|---|
| 1 | 90 | 98 | . | 188 |
| 2 | 80 | 98 | 70 | 248 |
| 3 | 20 | 90 | 30 | 140 |
After adding
| Maths | Physics | Chemistry | Science |
|---|---|---|---|
| 105 | 113 | . | 188 |
| 95 | 113 | 85 | 248 |
| 35 | 105 | 45 | 140 |
No comments:
Post a Comment