Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

datastep - SAS data step with BY variable on unsorted data

I'm executing SAS data step with by variable. I understand the output when the data is sorted by key (X in my case). However, when the data is unsorted, I get the following output:

SAS Output

I'm using SAS ODA's AFRICA dataset from MAPS library which has 52824 rows. Here's the link to the CSV file.

data  AFRICA_NEW12;
set Maps.AFRICA;
by X;
firstX = FIRST.X;
lastX = LAST.X;
run;

I don't understand how rows are selected when data is not sorted. Why does the output have 14 rows?

question from:https://stackoverflow.com/questions/65903367/sas-data-step-with-by-variable-on-unsorted-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have an error in your log because you didn't sort it. Make sure to read your log.

This likely generates the same issue for you:

data cars;
set sashelp.cars;
by model;
run;

proc print data=cars;
var make model origin;
run;

Output is:

Obs Make    Model   Origin
1   Acura   MDX Asia
2   Acura   RSX Type S 2dr  Asia

And the log shows:

 ERROR: BY variables are not properly sorted on data set SASHELP.CARS.
 Make=Acura Model=TSX 4dr Type=Sedan Origin=Asia DriveTrain=Front MSRP=$26,990 Invoice=$24,647 EngineSize=2.4 Cylinders=4
 Horsepower=200 MPG_City=22 MPG_Highway=29 Weight=3230 Wheelbase=105 Length=183 FIRST.Model=1 LAST.Model=1 _ERROR_=1 _N_=3
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: There were 4 observations read from the data set SASHELP.CARS.
 WARNING: The data set WORK.CARS may be incomplete.  When this step was stopped there were 2 observations and 15 variables.
 WARNING: Data set WORK.CARS was not replaced because this step was stopped.

Note this portion specifically:

WARNING: The data set WORK.CARS may be incomplete. When this step was stopped there were 2 observations and 15 variables.

If you know the data is sorted in the order you want, which may not be the same as what SAS expects you can add the notsorted option on the BY statement but this is a different type of functionality so check your code thoroughly.

data cars;
set sashelp.cars;
by model notsorted;
run;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...