BookmarkSubscribeRSS Feed
Eileen1496
Obsidian | Level 7

Hi everyone,

I'm new to SAS. I was trying the order by function using a dataset from sashelp library (sashelp.cars). I want to sort by msrp in ascending order. However, as I run my code, it does not sort msrp in ascending order. I could not find any mistakes in my code. 

Can you help me find out what happens?

Thank you!

 

Code:

proc sql;
select cars.make, cars.model, cars.msrp as tax
from sashelp.cars
where msrp<=40000;
order by msrp;
quit;
4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hi @Eileen1496 and welcome to the SAS Support Communities!

 

Remove the semicolon after the WHERE clause. Otherwise the ORDER BY clause is not part of the query and causes a syntax error (did you see the message in the log?).

Eileen1496
Obsidian | Level 7

Thank you, it indeed solves the problem!

ballardw
Super User

It is best to show the LOG when discussing code.

In the case of what you posted it clearly shows an error.

 proc sql;
8    select cars.make, cars.model, cars.msrp as tax
9    from sashelp.cars
10   where msrp<=40000;
NOTE: Writing HTML Body file: sashtml.htm
11   order by msrp;
     -----
     180
ERROR 180-322: Statement is not valid or it is used out of proper order.

12   quit;

The semicolon at the end of the Where clause ends the SELECT. So the Order By starts a new statement and is not legal as the first clause of a query.

Remove the semicolon after the Where msrp<=40000

 


@Eileen1496 wrote:

Hi everyone,

I'm new to SAS. I was trying the order by function using a dataset from sashelp library (sashelp.cars). I want to sort by msrp in ascending order. However, as I run my code, it does not sort msrp in ascending order. I could not find any mistakes in my code. 

Can you help me find out what happens?

Thank you!

 

Code:

proc sql;
select cars.make, cars.model, cars.msrp as tax
from sashelp.cars
where msrp<=40000;
order by msrp;
quit;

 

Eileen1496
Obsidian | Level 7

Thank you! You are right it is this problem. 

This time since it still gave me output, so I did not think there would be a problem and did not check the log. In the log, there is indeed this error. Next time I would check log anyway!

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 531 views
  • 1 like
  • 3 in conversation