Hello!
For an assignment, I have a data set that I have to round number grades, and then use array and do statements to assign a letter grade.
So far I have
data hwweek4;
set sasuser.detail;
Math1=round(Math, 1);
Science1=round(Science, 1);
PE1=round(PE, 1);
Reading1=round (reading, 1);
drop math science pe reading;
run;
Now what I need to do with this data set is to create new columns with a grade for each of these subjects using array and do statements.
If grade is above 90 then Grade= A
If grade is 75-79 then Grade is a B
If Grade is 60-74 then Grade is C
Below 60 but not missing is a D
I took the example sashelp.class and developed the code as per your requirement.
Please check and update as per your data and test
data class;
set sashelp.class;
array grades(2)$ heightgrade weightgrade;
array result1(2) height weight;
array result2(2) height2 weight2;
do i = 1 to 2;
if result1(i) ne . then result2(i)=round(result1(i),1);
if result2(i)>90 then grades(i)='A';
else if 75<=result2(i)<=89 then grades(i)='B';
else if 60<=result2(i)<=74 then grades(i)='C';
else if .<result2(i)<60 then grades(i)='D';
end;
run;
1. Create two arrays for your subjects numerical grade and one for their alphabetical grade
2. Write a series of IF statements for one variable first
2. Add a DO loop around it.
4. Change the one variable to be arrayName(i) instead.
Or do this in one line with a format.
@klj81 wrote:
Hello!
For an assignment, I have a data set that I have to round number grades, and then use array and do statements to assign a letter grade.
So far I have
data hwweek4;
set sasuser.detail;
Math1=round(Math, 1);
Science1=round(Science, 1);
PE1=round(PE, 1);
Reading1=round (reading, 1);
drop math science pe reading;
run;
Now what I need to do with this data set is to create new columns with a grade for each of these subjects using array and do statements.
If grade is above 90 then Grade= A
If grade is 75-79 then Grade is a B
If Grade is 60-74 then Grade is C
Below 60 but not missing is a D
Thanks for the reply. I am very new to SAS and not sure how to create the arrays or do loop.
I have been trying to find examples of how to write the code and the proper syntax but have been trouble finding one.
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
@klj81 wrote:
Thanks for the reply. I am very new to SAS and not sure how to create the arrays or do loop.
I have been trying to find examples of how to write the code and the proper syntax but have been trouble finding one.
I took the example sashelp.class and developed the code as per your requirement.
Please check and update as per your data and test
data class;
set sashelp.class;
array grades(2)$ heightgrade weightgrade;
array result1(2) height weight;
array result2(2) height2 weight2;
do i = 1 to 2;
if result1(i) ne . then result2(i)=round(result1(i),1);
if result2(i)>90 then grades(i)='A';
else if 75<=result2(i)<=89 then grades(i)='B';
else if 60<=result2(i)<=74 then grades(i)='C';
else if .<result2(i)<60 then grades(i)='D';
end;
run;
This worked perfectly for me..
thank you so much I really appreciate your help!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.