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
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;
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 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
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.