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

I am trying to create a new variable LetterGrade with recoding GPA into alphabets.

 

However, I keep recieving the message saying variable uninitialized. Below is my code in SAS Studio

 

Please Help!

 

*Creating new variable based on Letter grade;
data Lgrades;
set students_additional;
if 0<gpa<0.5 then LetterGrade=F;
else if 0.5<gpa<1.5 then LetterGrade=D;
else if 1.5<gpa<2.5 then LetterGrade=C;
else if 2.5<gpa<3.5 then LetterGrade=B;
else if gpa >=3.5 then LetterGrade=A;
RUN;
proc print data=Lgrades;
title 'LetterGrade Dataset';
run;
proc contents data=Lgrades;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
nikky
Fluorite | Level 6

Thank you , I will do right away.

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Hi @nikky 

You would need to make recoded values a constant operand within "quotes" rather than variable operand.

 

For eg

if 0<gpa<0.5 then LetterGrade="F";
nikky
Fluorite | Level 6

Thank you , I will do right away.

Tom
Super User Tom
Super User

@nikky wrote:

I am trying to create a new variable LetterGrade with recoding GPA into alphabets.

 

However, I keep recieving the message saying variable uninitialized. Below is my code in SAS Studio

 

Please Help!

 

*Creating new variable based on Letter grade;
data Lgrades;
set students_additional;
if 0<gpa<0.5 then LetterGrade=F;
else if 0.5<gpa<1.5 then LetterGrade=D;
else if 1.5<gpa<2.5 then LetterGrade=C;
else if 2.5<gpa<3.5 then LetterGrade=B;
else if gpa >=3.5 then LetterGrade=A;
RUN;
proc print data=Lgrades;
title 'LetterGrade Dataset';
run;
proc contents data=Lgrades;
run;

 


What values do the variables A, B, C and D have? 

Or did you mean to set the variable to the character stings 'A', 'B', etc instead?

nikky
Fluorite | Level 6

Yes Tom, I do mean to have them in character form.

 

 

Thank you

ballardw
Super User

@nikky wrote:

I am trying to create a new variable LetterGrade with recoding GPA into alphabets.

 

However, I keep recieving the message saying variable uninitialized. Below is my code in SAS Studio

 

Please Help!

 

*Creating new variable based on Letter grade;
data Lgrades;
set students_additional;
if 0<gpa<0.5 then LetterGrade=F;
else if 0.5<gpa<1.5 then LetterGrade=D;
else if 1.5<gpa<2.5 then LetterGrade=C;
else if 2.5<gpa<3.5 then LetterGrade=B;
else if gpa >=3.5 then LetterGrade=A;
RUN;
proc print data=Lgrades;
title 'LetterGrade Dataset';
run;
proc contents data=Lgrades;
run;

 


So what grade do you want to assign with the GPA is exactly 0, 0.5, 1.5 or 2.5 ? You are excluding the end point in every single one of your comparisons with those values. I suspect that you may want

data Lgrades;
set students_additional;
  if 0<gpa<=0.5 then LetterGrade='F';
  else if 0.5<gpa<=1.5 then LetterGrade='D';
  else if 1.5<gpa<=2.5 then LetterGrade='C';
  else if 2.5<gpa<3.5 then LetterGrade='B';
 else if gpa >=3.5 then LetterGrade='A';
 RUN;

or similar

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 5 replies
  • 5866 views
  • 0 likes
  • 4 in conversation