BookmarkSubscribeRSS Feed
sjbothwell
Calcite | Level 5

Hi! 

I am trying to sort data by Subject_ID (primary) and Lab_Date. When I run the following code the data doesn't appear to be sorted : 

PROC SORT DATA = WORK.Laboratory_2015;
      BY Subject_ID Lab_Date;
RUN;

 

My data looks like this : 

sjbothwell_0-1604446098269.png

 

8 REPLIES 8
SASKiwi
PROC Star

Please post the SAS log of your PROC SORT.

sjbothwell
Calcite | Level 5

My initial code was 

PROC SORT DATA = CanImpt.Laboratory_2015;
BY Subject_ID Lab_Date;
RUN;

Where CanImpt was a file path on my computer and the log gave this error 

 

ERROR: You cannot open CANIMPT.LABORATORY_2015.DATA for output access with member-level control because CANIMPT.LABORATORY_2015.DATA is in use by you in resource environment SORT.

 

I changed CanImpt to WORK for the post but just realized that by using 

DATA WORK.Laboratory_2015;
      SET CanImpt.Laboratory_2015;
RUN;

and then sorting off of WORK.Laboratory_2015 works. Why do I need a temporary dataset? Or is there a better method?

 

 

 

 

SASKiwi
PROC Star

@sjbothwell  - Looks like you have a process already locking your permanent dataset so you can't overwrite it. Try closing your SAS session, reopen it and try the sort again.

sjbothwell
Calcite | Level 5

That did the trick - thank you!

novinosrin
Tourmaline | Level 20

Hi @sjbothwell  That seems strange. Can you try writing the sorted contents to new dataset and compare-

PROC SORT DATA = WORK.Laboratory_2015 out=sorted_lab_2015;
      BY Subject_ID Lab_Date;
RUN;

Please open both sorted_lab_2015 and WORK.Laboratory_2015 and eyeball 

sjbothwell
Calcite | Level 5

That worked thank you!

ballardw
Super User

The result looks like you actually ran at some point:

 

PROC SORT DATA = WORK.Laboratory_2015;
BY Lab_Date Subject_ID;
RUN;

 

If you did sort this way and the data set was open for some reason then a different sort may not have been done.

 

Deanna_Payne
Obsidian | Level 7

Like the earlier commenters, double check the datasets you are referencing and make sure that they work in your log and are correctly named.

 

I didn't see if you named the dataset into your log (Make sure you are not missing something like this):

 

DATA WORK.LABORATORY_2015;
SET CANIMPT.LABORATORY_2015;
RUN;

 

Then run the proc sort statement:


PROC SORT DATA = WORK.LABORATORY_2015;
BY SUBJECT_ID LAB_DATE;
RUN;

 

But if that is still not working you can achieve the same grouping result in a different way:

PROC PRINT DATA = WORK.LABORATORY_2015;
BY SUBJECT_ID LAB_DATE;
RUN;

 

While this method isn't great for large datasets with a lot of unique variables of Subject_ID, Lab_Date, it is good for seeing trends in a more bitesized way.

 

Hope this helps