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

How do I get the distinct values of a variable without it being sorted alphabetically for me?
I would like to retain the order of the values in the initial table.

1 ACCEPTED SOLUTION
4 REPLIES 4
KachiM
Rhodochrosite | Level 12

Hi @sustagens 

 

If your aim is to get a list of Distinct values of a Variable without sorting and changing the order of values, you use Proc Sql. To get the list of distinct AGEs from SASHELP.CLASS use:

 

proc sql noprint;
   select distinct (Age) into :dList separated by ' '
      from sashelp.class
   ;
quit;
%put &dList;

Similarly, you can do for a Character Variable.

Kurt_Bremser
Super User

And if you can fit the variable values for de-duplication in memory, a single data step with a hash can do it.

data want;
set have;
if _n_ = 1
then do;
  declare hash lookup;
  lookup.definekey("key");
  lookup.definedone();
end;
if lookup.check() ne 0;
rc = lookup.add();
drop rc;
run;

Where key is the variable used for de-duplication. If needed, more variables can be added in the definekey() method.

 

Code is not tested, as I'm posting from my tablet.

Ksharp
Super User

Or you could try PROC FREQ + ORDER= option.

 

data class;
set sashelp.class;
run;

proc freq data=class noprint order=data;
table age/out=want nopercent nocum;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 1835 views
  • 1 like
  • 4 in conversation