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;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2944 views
  • 1 like
  • 4 in conversation