BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ellpap
Calcite | Level 5

Hi,

I am having some issues with an array and do loop. 

CODE:

data lettergrade;
set detailrounded;
array english_array[*] _numeric_;
do i = 1 to dim(english_array);
if english>=90 then englishgrade="A";
if english>=75 and english<=89 then englishgrade="B";
if english>=60 and english<=74 then englishgrade="C";
if english<=60 then englishgrade="D";
end;

 

Some of my values are missing and I did not want SAS to automatically assign a letter grade of "D". Is there anyway to exclude any missing data points so no letter grade shows up? 
Let me know if that makes sense or if clarification is needed! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

if 0 <= english_grade[i]<=60 then englishgrade="D";

 

would be the approach.

If you actually have more than one variable your "grade" is only going to be for the last variable. If you need more then you need to have a matching character array.

I have to assume you meant to use the english_grade[i] otherwise you are repeating the exact same code for the variable "english" not the ones in the array.

 

Another approach for reporting purposes is to create a custom format such as:

 

Proc format;
value grade
90 - high = 'A'
75 - 89    = 'B'
60 - 74    = 'C'
0   -<60   = 'D'
other       = ' '
;
run;

Then when you print or display the value associate the format with the variable:

 

 

 

proc print data=detailedrounded;
   var _numeric_;
   format _numeric_ grade. ;
run;

 

 

The SAS formats are a very powerful part of SAS. Any display for a single variable can use this. One advantage is you could have multiple formats , such gradecurve, that uses a slightly different range. The only change you would need is to associate the format with the variable(s) of interest.

Groups created by formats are used by most of the analysis, graphing and reporting procedures.

Try

proc freq data=detailedrounded;
   tables _numeric_;
   format _numeric_ grade. ;
run;

to see an example (after running the proc format code to make the format).

You do need to make sure the format is available in the current session such as run the format code before use.

View solution in original post

1 REPLY 1
ballardw
Super User

if 0 <= english_grade[i]<=60 then englishgrade="D";

 

would be the approach.

If you actually have more than one variable your "grade" is only going to be for the last variable. If you need more then you need to have a matching character array.

I have to assume you meant to use the english_grade[i] otherwise you are repeating the exact same code for the variable "english" not the ones in the array.

 

Another approach for reporting purposes is to create a custom format such as:

 

Proc format;
value grade
90 - high = 'A'
75 - 89    = 'B'
60 - 74    = 'C'
0   -<60   = 'D'
other       = ' '
;
run;

Then when you print or display the value associate the format with the variable:

 

 

 

proc print data=detailedrounded;
   var _numeric_;
   format _numeric_ grade. ;
run;

 

 

The SAS formats are a very powerful part of SAS. Any display for a single variable can use this. One advantage is you could have multiple formats , such gradecurve, that uses a slightly different range. The only change you would need is to associate the format with the variable(s) of interest.

Groups created by formats are used by most of the analysis, graphing and reporting procedures.

Try

proc freq data=detailedrounded;
   tables _numeric_;
   format _numeric_ grade. ;
run;

to see an example (after running the proc format code to make the format).

You do need to make sure the format is available in the current session such as run the format code before use.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 869 views
  • 0 likes
  • 2 in conversation