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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Side note: you should use

 

GradeNu3=.

 

or

 

call missing(GradeNu3)

 

to assign a missing value to GradeNu3.

PG

View solution in original post

10 REPLIES 10
PGStats
Opal | Level 21

Are you missing a DATA statement at the beginning?

PG
mcgannmary1
Obsidian | Level 7

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
17
6.85
14.05
100.00
26
10.48
21.49
100.00
78
31.45
64.46
100.00
0
0.00
0.00
0.00
0
0.00
0.00
0.00
0
0.00
0.00
.
121
48.79
 
 
0
0.00
0.00
0.00
0
0.00
0.00
0.00
0
0.00
0.00
0.00
1
0.40
100.00
100.00
0
0.00
0.00
0.00
0
0.00
0.00
.
1
0.40
 
 
0
0.00
0.00
0.00
0
0.00
0.00
0.00
0
0.00
0.00
0.00
0
0.00
0.00
0.00
126
50.81
100.00
100.00
0
0.00
0.00
.
126
50.81
 
 
17
6.85
26
10.48
78
31.45
1
0.40
126
50.81
0
0.00
248
100.00

 

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!!

mcgannmary1
Obsidian | Level 7

This is only slightly better.

 

The SAS System

 

The FREQ Procedure

Frequency

Percent

Row Pct

Col Pct

Table of GradeNu3 by Grade

GradeNu3

Grade(Grade)

10th

11th

12th

9th

College/Technical
school

Ungraded

Total

1

17

6.85

14.05

100.00

26

10.48

21.49

100.00

78

31.45

64.46

100.00

0

0.00

0.00

0.00

0

0.00

0.00

0.00

0

0.00

0.00

.

121

48.79

 

 

2

0

0.00

0.00

0.00

0

0.00

0.00

0.00

0

0.00

0.00

0.00

1

0.40

100.00

100.00

0

0.00

0.00

0.00

0

0.00

0.00

.

1

0.40

 

 

5

0

0.00

0.00

0.00

0

0.00

0.00

0.00

0

0.00

0.00

0.00

0

0.00

0.00

0.00

126

50.81

100.00

100.00

0

0.00

0.00

.

126

50.81

 

 

Total

17

6.85

26

10.48

78

31.45

1

0.40

126

50.81

0

0.00

248

100.00

Frequency Missing = 29

 

PGStats
Opal | Level 21

Side note: you should use

 

GradeNu3=.

 

or

 

call missing(GradeNu3)

 

to assign a missing value to GradeNu3.

PG
mcgannmary1
Obsidian | Level 7

Thank you!

 

Reeza
Super User
I don't think alphabetical comparisons will give you want you want. Those are character values so it will sort/order less 11<2 for example.

As PgStats indicate, you're msising a data statement as well as a DATA= on your PROC PRINT. Which data set is your PROC PRINT referring to? How do you know that?
mcgannmary1
Obsidian | Level 7

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

Reeza
Super User
For equality comparisons that's totally fine, but for inequalities it's not. Your first line is an inequality:

If Gradeb <= '8th' then GradeNu3=1;

You may get unexpected behaviour with that line. Test your re-coding thoroughly.

ballardw
Super User

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.

mcgannmary1
Obsidian | Level 7

This is extremely helpful!

Thanks for taking the time to share this.

Best,

Mary

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1086 views
  • 4 likes
  • 4 in conversation