BookmarkSubscribeRSS Feed
daft22
Fluorite | Level 6

Good Evening,

I am trying to alter a table which has homework scores by replacing the numerical scores with letter grades. 
My Current code looks as such:

DATA HW;
INFILE '/folders/myfolders/sasuser.v94/HomeworkData.txt' FIRSTOBS=2;
INPUT Name :$8. Homework1 Homework2 Homework3 Homework4 Homework5;
RUN;

PROC PRINT DATA=HW;
RUN;

DATA HW;
IF Homework1 >= 90 THEN DO;
Homework1 = 'A';
END;

RUN;

PROC PRINT DATA=HW;
RUN;

 

So far, none of the data in the first column that has a numerical value of 90 or higher is actually converting into an 'A' grade.
Am I missing something?

Thank you!!

 

PS. First column is for the names of the 'students' 

2 REPLIES 2
PGStats
Opal | Level 21

SAS provides FORMATS as a way to display letter grades AND to keep the underlying numbers. Try:

 

proc format;
value grade
low - 49 = "F"
50 - 59 = "E"
60 - 69 = "D"
70 - 79 = "C"
80 - 89 = "B"
90 - high = "A";
run;

DATA HW;
INFILE '/folders/myfolders/sasuser.v94/HomeworkData.txt' FIRSTOBS=2;
INPUT Name :$8. Homework1 - Homework5;
format Homework1 - Homework5 grade.;
RUN;

proc print data=HW; run;

That way you can count the number of students for each letter grade in a class (with proc freq) but also the average grade for the class (proc means), with the same data.

PG
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @daft22 

 

In your posted code you don't have a set statement in your recode step, so instead of recoding values in the original data set HW, you replace the existing HW with a new data set with one observation and one variable containing a missing value.

 

But you refer to "none of the data", which means that you ran your code with a set statement, but didn't get the desired result. That is because Homework1 is already defined as numeric, so you cannot change it to 'A'. You get a missing value and a note about invalid numeric data. If you want to recode data instead of using a format, you must create new grade-variables to hold the character values. 

 

Please always post the log. I gues it looks like this:

21
22   DATA HW; set HW;
23   IF Homework1 >= 90 THEN DO;
24   Homework1 = 'A';
25   END;
26   RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      24:13
NOTE: Invalid numeric data, 'A' , at line 24 column 13.
Homework1=. Homework2=30 _ERROR_=1 _N_=1
NOTE: There were 1 observations read from the data set WORK.HW.
NOTE: The data set WORK.HW has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           4.55 seconds
      cpu time            0.14 seconds

 

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 989 views
  • 2 likes
  • 3 in conversation