Hi,
I'm trying to run what seems like pretty basic code, but continually get an error.
Here is the code:
set OAH_BL;
Gradeb=left(trim(Grade));
If Gradeb<='8th' then GradeNu3=1;
else if Gradeb='9th' or Gradeb='10th' then GradeNu3=2;
else if Gradeb='11th' or Gradeb='12th' then GradeNu3=3;
else if Gradeb=' ' then GradeNu3='.';
else if Gradeb='High school graduate or GED' then GradeNu3=4;
else if Gradeb='College/Technical school' then GradeNu3=5;
run;
proc print;
var GradeNu3;
run;
Here is the log:
NOTE: The SAS System stopped processing this step because of errors.
229 If Gradeb<='8th' then GradeNu3=1;
--
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
230 else if Gradeb='9th' or Gradeb='10th' then GradeNu3=2;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
231 else if Gradeb='11th' or Gradeb='12th' then GradeNu3=3;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
232 else if Gradeb=' ' then GradeNu3='.';
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
233 else if Gradeb='High school graduate or GED' then GradeNu3=4;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
234 else if Gradeb='College/Technical school' then GradeNu3=5;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
235 run;
236
237 proc print;
238 var GradeNu3;
ERROR: Variable GRADENU3 not found.
239 run;
Any help would be appreciated.
Best,
Mary
Side note: you should use
GradeNu3=.
or
call missing(GradeNu3)
to assign a missing value to GradeNu3.
Are you missing a DATA statement at the beginning?
Yes, indeed I did! Thank you for catching that.
I put that in and re-ran this code:
/*Create a new file*/
data oahbl_Recode_20190620;
/*Set the file to be accessed*/
set OAH_BL;
Gradeb=left(trim(Grade));
If Gradeb<='8th' then GradeNu3=1;
else if Gradeb='9th' or Gradeb='10th' then GradeNu3=2;
else if Gradeb='11th' or Gradeb='12th' then GradeNu3=3;
else if Gradeb=' ' then GradeNu3=.;
else if Gradeb='High school graduate or GED' then GradeNu3=4;
else if Gradeb='College/Technical school' then GradeNu3=5;
run;
And end up with the output below:
Table of GradeNu3 by GradeGradeNu3 Grade(Grade)10th 11th 12th 9th College/Technicalschool Ungraded Total125TotalFrequency Missing = 29
|
I checked the raw data and there should be records in groups 3 and 4.
Thank you and please excuse the centering of my desk!!
This is only slightly better.
The SAS System |
The FREQ Procedure
|
|
Side note: you should use
GradeNu3=.
or
call missing(GradeNu3)
to assign a missing value to GradeNu3.
Thank you!
I was trying to convert the alphabetic characters to numeric. Are you saying that converting the alphanumeric variables to numeric isn't possible with a statement like this,
else if Gradeb='9th' or Gradeb='10th' then GradeNu3=2;
How else might I go about creating a numeric variable from this Gradeb variable?
Thank you!
Mary
One approach is to use an INFORMAT to convert text to numeric.
proc format library = work; invalue GradeNu (upcase) '8TH' = 1 '9TH','10TH'=2 '11TH','12TH'=3 'HIGH SCHOOL GRADUATE OR GED' =4 'COLLEGE/TECHNICAL SCHOOL' =5 ' '=. other= 1 /* assumes all the others are < 8th*/ ; data want ; set have; gradenu = input(gradeb,gradenu.)); run;
without seeing actual values I would actually tend to actually list the grades "<" 8th grade and use OTHER=_error_ instead.
Note the UPCASE instruction will convert the text to uppercase before comparing to the listed values. Which may help if you have some poor data entry and have a mix of "High School" "HIGH SCHOOL" "High school". With if/then/else you have to list every single occurring combinations of case.
The other=_error_ would put an invalid data message in the log in case someone sneaks in an unexpected value such as "Graduate School" or "Vocational/Technical". If that happens you just add the appropriate text to the INVALUE and rerun the data step. No need to add additional If/then/else statements. Since I deal with data where my providers randomly make changes such as from "Returned to work or school" to "Returned to work" I have several informats like this to warn me of changed data entry.
This is extremely helpful!
Thanks for taking the time to share this.
Best,
Mary
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.