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

I'm trying to do the following with PROC SORT:

 

DATA FOO;
  INPUT name $;
  DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
  SELECT *
  FROM FOO
  ORDER BY CASE name
    WHEN 'westchester' THEN 1
    WHEN 'putnam' THEN 2
    WHEN 'suffolk' THEN 3
    WHEN 'nassau' THEN 4
  END;
QUIT;


 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You'd still need to create those new variables in a prior step and then sort accordingly. 

 

AFAIK there's no way to incorporate that into the PROC SORT directly, which is what I assume you're asking.

 


@tomcmacdonald wrote:

I'm trying to do the following with PROC SORT:

 

DATA FOO;
  INPUT name $;
  DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
  SELECT *
  FROM FOO
  ORDER BY CASE name
    WHEN 'westchester' THEN 1
    WHEN 'putnam' THEN 2
    WHEN 'suffolk' THEN 3
    WHEN 'nassau' THEN 4
  END;
QUIT;


 


 

View solution in original post

3 REPLIES 3
Reeza
Super User

You'd still need to create those new variables in a prior step and then sort accordingly. 

 

AFAIK there's no way to incorporate that into the PROC SORT directly, which is what I assume you're asking.

 


@tomcmacdonald wrote:

I'm trying to do the following with PROC SORT:

 

DATA FOO;
  INPUT name $;
  DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
  SELECT *
  FROM FOO
  ORDER BY CASE name
    WHEN 'westchester' THEN 1
    WHEN 'putnam' THEN 2
    WHEN 'suffolk' THEN 3
    WHEN 'nassau' THEN 4
  END;
QUIT;


 


 

ballardw
Super User

And the question is?

"I'm trying to do the following the PROC SORT" doesn't make much sense.

You need to check your example data. The way you read your data you do not have a value of "Westchester" only "westches" . $ without an informat or similar to tell SAS the variable is longer than 8 characters results in truncated data.

DATA foo;
  INPUT name $12.;
  DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

If you were not getting expected results you should show the results you were getting and what you expected

 

 

Are you attempting to use the proc sql instead of proc sort?

tomcmacdonald
Quartz | Level 8
Sorry, I didn't test the code above. I'm trying to sort based on an assigned priority of a categorical variable solely using PROC SORT without having to create a temporary variable.

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