BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

Hi,

 

The two examples below gives the same result: sortedby includes the sorting order.

Capture1.JPG

But when the dataset is modified (add the commented dataset to test it - additional variable could also be added - so proc copy would not be sufficient), the sorted order is lost.

 

Capture2.JPG

Is this meant to be. Is there a way to avoid it?

 

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

/*
data class;
    set class;
run;*/

proc sql;
    select name, sortedby
    from dictionary.columns
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

*---------------------------------------------;
data class (sortedby=sex descending name);
    set class ;
run;

/*
data class;
    set class;
run;*/

proc sql;
    select name, sortedby
    from dictionary.columns
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The sortedby option is a writable data set attribute that doesn't ensure that the data is actually sorted this way. 

Creating a table using Proc Sort ensures that the table is sorted.

IF you know your table is sorted and you want SAS to set the attribute for verified sorted then use Proc Sort with the PRESORTED option. With this option SAS will first verify if a table is already sorted and only actually sort the table if not confirmed.

Below code should explain it.

I'm not sure where the sortedby option adds value to processing as SAS will only use more efficient processing (like in a Proc SQL) if the sort order is verified - which is a table attribute you can't write directly.

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

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

data class (sortedby=sex descending name);
  set sashelp.class;
run;

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

proc sort data=work.class presorted;
  by sex descending name;
run;

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

Patrick_0-1658068621636.png

 

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

The sortedby option is a writable data set attribute that doesn't ensure that the data is actually sorted this way. 

Creating a table using Proc Sort ensures that the table is sorted.

IF you know your table is sorted and you want SAS to set the attribute for verified sorted then use Proc Sort with the PRESORTED option. With this option SAS will first verify if a table is already sorted and only actually sort the table if not confirmed.

Below code should explain it.

I'm not sure where the sortedby option adds value to processing as SAS will only use more efficient processing (like in a Proc SQL) if the sort order is verified - which is a table attribute you can't write directly.

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

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

data class (sortedby=sex descending name);
  set sashelp.class;
run;

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

proc sort data=work.class presorted;
  by sex descending name;
run;

proc sql;
    select memname, sorttype
    from dictionary.tables
    where upcase(libname)='WORK' and
          upcase(memname)='CLASS';
quit;

Patrick_0-1658068621636.png

 

xxformat_com
Barite | Level 11

I see your point. Having the information in the dataset metadata does not ensure that it reflects the actual order of the data.

 

Are you aware of a SAS Online Documentation listing all possible values for SORTTYPE?

Patrick
Opal | Level 21

@xxformat_com wrote:

I see your point. Having the information in the dataset metadata does not ensure that it reflects the actual order of the data.

 

Are you aware of a SAS Online Documentation listing all possible values for SORTTYPE?


I'm not but to my knowledge it's only verified or not verified - S or W

xxformat_com
Barite | Level 11

When searching on the net, I could find three other values but I don't know if there is any other one.

  • SK when using nodupkey in proc sort or distinct in proc sql
  • SR when using nodup in proc sort
  • O when removing the sorting
proc datasets lib = work nolist nodetails;
    modify class (sortedby = _NULL_);
    run;
quit;

 

Patrick
Opal | Level 21

Thanks for sharing.

%macro testIt(param);
  proc sort data=sashelp.class out=class &param;
    by sex descending name;
  run;
  title "Proc Sort with &param";
  proc sql;
      select memname, sorttype
      from dictionary.tables
      where upcase(libname)='WORK' and
            upcase(memname)='CLASS';
  quit;
  title;
%mend;

%testIt();
%testIt(nodupkey);
%testIt(noduprec);
%testIt(nouniquekey);

Patrick_0-1658582322847.png

 

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
  • 7 replies
  • 1089 views
  • 7 likes
  • 2 in conversation