Hi all SAS Users,
I am new to VIEW, even I am trying to read some documents about that but still a novice in that part.
Today I faced a problem named
ERROR: Cannot sort in place.
So, there are two scenarios, the first one (without views) it can run smoothly, the second one (with views) it runs with an error
The first scenario
data trim_price ;/*without views - this is the only difference between two codes*/
set temp;
if 0<rank<99 or year=1999;
run;
*sort by country;
proc sort data=work.trim_price;
by LOC year;
run;
The log
37 *removing stocks if yearly amihud 38 in the top 1% within a country each year; 39 40 *sort by country; 41 proc sort data=work.trim_price; 42 by LOC year; 43 run; NOTE: There were 52599 observations read from the data set WORK.TRIM_PRICE.
The second one
data trim_price / view=trim_price;
set temp;
if 0<rank<99 or year=1999;
run;
*sort by country;
proc sort data=work.trim_price;
by LOC year;
run;
The log
ERROR: Cannot sort in place. ERROR: Unable to create WORK.TRIM_PRICE.DATA because WORK.TRIM_PRICE.VIEW already exists.
Could you please explain to me the reason behind this? And whether this error is caused just because of the view or is there any possible hidden error?
Warmest regards and have a nice weekend.
@Phil_NZ - Really you don't need to ask. Just run it and if it works without error then of course that is fine. Updating in place with real datasets is perfectly OK.
Remember a view is essentially a read-only "window" into the original data behind it. If you wanted to change what you saw through a shop window would you change the window or what was behind it? The concept is the same.
Remember that Views are instructions on how to pull data from some other source.
So you can't change the order of the data in that instruction set.
You can specify an OUT= data set in proc sort but that may make the reason you are attempting to use a view moot.
If you know that you want a specific order in the set you might consider creating your view using Proc SQL with an Order By clause:
proc sql; create view sorted as select * from sashelp.class order by sex, age, weight ; quit; proc print data=sorted; run;
Hi @ballardw
Thank you for your suggestion, I am curious about that part
You can specify an OUT= data set in proc sort but that may make the reason you are attempting to use a view moot.
So, it equals to my first scenario( without view) regarding consuming the same storage, isn't it? I hope that I explain your idea properly.
Warm regards.
Sorting a view in-place doesn't make sense and isn't allowed. You've created the view based on a different table. If you want the data to come out in a different order sort the TEMP table and recreate the view.
If you want to sort the data coming out of the view use the OUT = option in your PROC SORT to store the sorted data in a different output dataset.
Hi @SASKiwi
If I want to keep the dataname, can I do that?
data trim_price ;/*without views - this is the only difference between two codes*/
set temp;
if 0<rank<99 or year=1999;
run;
*sort by country;
proc sort data=work.trim_price;
by LOC year;
run;
Warm regards.
@Phil_NZ - Really you don't need to ask. Just run it and if it works without error then of course that is fine. Updating in place with real datasets is perfectly OK.
Remember a view is essentially a read-only "window" into the original data behind it. If you wanted to change what you saw through a shop window would you change the window or what was behind it? The concept is the same.
If you want to create a view which returns the rows sorted then consider below:
proc sql feedback;
create view work.trim_price as
select *
from temp
where 0<rank<99 or year=1999
order by loc, year
;
quit;
You are sort the wrong thing. If you want to change the order the data is returned by the data step view then change the order of the order of the data that the view reads.
proc sort data=temp;
by LOC year;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.