BookmarkSubscribeRSS Feed
pinkyloop
Obsidian | Level 7

Hi all,i need help i am totally new to sas and still trying to figure how things work!

 

i am trying to create a new variable  with multiple observation  but i keep getting syntax error.

here is what i am trying to solve.

 

Thanks

a new variable called LetterGrade that reports an A if the GPA is
3.5 or higher, a B is the GPA is greater than equal to 2.5 but less than 3.5, a
C if the GPA is greater than or equal to 1.5 but less than 2.5, a D ifthe GPA   is
greater than or equal 0.5 but less than 1.5, and an F if the GP is below 0.5
 
data merged.lettergrade;
set merged.merged;
lettergrade=(A=(gpa>3.5) B=(gpa 2.5>=3.5) C=(gpa 1.5>=2.5) D=(gpa 0.5>=1.5) F=(gpa<0.5));
run;
proc print data=merged.lettergrade;
run;
4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Community 🙂

 

This king of classification problem is solved smoothly by a PROC FORMAT like this

 

proc format;
	value fmt 
		low -< 0.5 = 'F'
		0.5 -< 1.5 = 'D'
		1.5 -< 2.5 = 'C'
		2.5 -< 3.5 = 'B'
		3.5 - high = 'A'
	;
run;

data have;
	do x=0 to 5 by .5;
		output;
	end;
run;

data want;
	set have;
	y=put(x, fmt.);
run;

Feel free to ask if you have any questions 

pinkyloop
Obsidian | Level 7
this looks kind of advanced for me but will save it for when i get to this level.Thank you so much .i appreciate
KachiM
Rhodochrosite | Level 12

@pinkyloop Welcome to Community SAS.

 

SAS Data step needs several IF - THEN conditions. You can't place all together. Here is another way for the beginner.

data grade;
input gpa;
datalines;
0.3
3.5
4.0
2.5
1.5
3.2
;
run;

data aa;
   set grade;
   /*lettergrade=(A=(gpa>3.5) B=(gpa 2.5>=3.5) C=(gpa 1.5>=2.5) D=(gpa 0.5>=1.5) F=(gpa<0.5)); */
   if (gpa>3.5) then lettergrade='A';
   else if (gpa>2.5) then lettergrade='B';
   else if (gpa>1.5) then lettergrade='C';
   else if (gpa>0.5) then lettergrade='D';
   else lettergrade='F';
run;

Another way is to use SELECT-WHEN switch statement as:

 

data bb;
   set grade;
   select;
      when (gpa > 3.5) Lettergrade='A';
      when (gpa > 2.5) Lettergrade='B';
      when (gpa > 1.5) Lettergrade='C';
      when (gpa > 0.5) Lettergrade='D';
      otherwise Lettergrade='F';
   end;
run;

All the best to learn SAS programming

pinkyloop
Obsidian | Level 7
it worked!!i i thought the "if" statement will create different variables.but now its clearer.Thank you so much