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

Hi All,

 

I have two data steps, one in Proc SQL the other in bases SAS. Both of the codes are sourcing the exact same variables from the same source and are being sorted by the same variable.

proc sql;
	create table vals_base as
	select 
	ACCTAPPLNUMBER as application_id,
	VALUATIONDATE,
	DIM_CO_VALUATION_SSK,	
	VALUATIONTYPE,
	LASTMODIFIEDDATE_DATE,
	NUMBEROFBATHROOMS,
	NUMBEROFBEDROOMS,
	PROPERTYBUILT,
	VALUEINCURRENTCONDITION,	
	PROPERTYTYPE,
	PROPERTYSUBTYPE
	from TEST_APP.DPR_DIM_APPLICATION_VALUATIONS
	order by ACCTAPPLNUMBER;
quit;
proc sort data=test_app.DPR_DIM_APPLICATION_VALUATIONS (keep = DIM_CO_VALUATION_SSK 
              VALUATIONDATE VALUATIONTYPE VALUEINCURRENTCONDITION LASTMODIFIEDDATE_DATE
              NUMBEROFBATHROOMS NUMBEROFBEDROOMS PROPERTYBUILT ACCTAPPLNUMBER
              PROPERTYTYPE PROPERTYSUBTYPE )  out=vals_base (rename = (ACCTAPPLNUMBER = application_id));
 by ACCTAPPLNUMBER;
run;

Why would the outputs differ in sort beyond the sort key. For example the valuationtype variable is outputted different in both outputs although it is not listed as a variable  to be sorted.

 

 

Base SAS output

 

application_idVALUATIONDATEVALUATIONTYPE
153828FEB2014:00:00:00Quest - Homebuyers
153817FEB2014:00:00:00Quest - Homebuyers
153828FEB2014:00:00:00Quest - Full Valuation

 

 

Proc SQL output

 

application_idVALUATIONDATEVALUATIONTYPE
153828FEB2014:00:00:00Quest - Full Valuation
153817FEB2014:00:00:00Quest - Homebuyers
153828FEB2014:00:00:00Quest - Homebuyers

 

The source table does contain multiple application Id's.

 

Thanks

 

Adnan

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

The secondary sort order is not guranteed in sql. Well, the sort order is never guranteed in sql regardless unless you force or specify a vars in order by clause like 1,2,3 to get desired

However proc sort/datastep are sequential processing. Therefore spits out the order that exists in the dataset that is by read. 

 

SQL is very tricky in general to imagine how the sql processor would work for a particular logic

 

A simpler example to illustrate:

 

proc sql;
create table w as
select *
from sashelp.class
order by sex;
quit;

 

SAS Output

Name Sex Age Height Weight
JudyF1464.390
JaneF1259.884.5
JoyceF1151.350.5
BarbaraF1365.398
CarolF1462.8102.5
MaryF1566.5112
LouiseF1256.377
AliceF1356.584
JanetF1562.5112.5

 

proc sort data=sashelp.class out=w1;
by sex;
run;

SAS Output

Obs Name Sex Age Height Weight1234567
AliceF1356.584.0
BarbaraF1365.398.0
CarolF1462.8102.5
JaneF1259.884.5
JanetF1562.5112.5
JoyceF1151.350.5
JudyF1464.390.0

 

 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

The secondary sort order is not guranteed in sql. Well, the sort order is never guranteed in sql regardless unless you force or specify a vars in order by clause like 1,2,3 to get desired

However proc sort/datastep are sequential processing. Therefore spits out the order that exists in the dataset that is by read. 

 

SQL is very tricky in general to imagine how the sql processor would work for a particular logic

 

A simpler example to illustrate:

 

proc sql;
create table w as
select *
from sashelp.class
order by sex;
quit;

 

SAS Output

Name Sex Age Height Weight
JudyF1464.390
JaneF1259.884.5
JoyceF1151.350.5
BarbaraF1365.398
CarolF1462.8102.5
MaryF1566.5112
LouiseF1256.377
AliceF1356.584
JanetF1562.5112.5

 

proc sort data=sashelp.class out=w1;
by sex;
run;

SAS Output

Obs Name Sex Age Height Weight1234567
AliceF1356.584.0
BarbaraF1365.398.0
CarolF1462.8102.5
JaneF1259.884.5
JanetF1562.5112.5
JoyceF1151.350.5
JudyF1464.390.0

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 605 views
  • 0 likes
  • 3 in conversation