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

Hi Everyone,

I have a dataset with n variables. I want to create a summary file of distinct/unique values for each variables.


data have;

  input a0 a1 a2 a3 a4 a5 a6;

  datalines;

0 5 9 1 0 8 1

1 4 0 1 0 5 0

1 8 0 1 0 1 1

;

as you see, a0 only take value 0 or 1; a1 values are 5 4 8; a2 values are 9 and 0; a3 take only value 1...

The output file should be:
a0 a1 a2 a3 a4 ...
1  5  9  1  0...
0  4  0
   8


I wonder if there is any simple way to get it done. Right now I can only do it through sort nondupkey one by one and merge, which is not efficient.

Thank you so much for your help.

HHC

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

This looks about right.

data have;
  input a0 a1 a2 a3 a4 a5 a6;
  datalines;
0 5 9 1 0 8 1
1 4 0 1 0 5 0
1 8 0 1 0 1 1
;;;;
   run;
proc summary data=have missing chartype;
  
class a:;
   ways 1;
  
output out=distinct(drop=_type_ _freq_) / levels;
  
run;
proc print;
  
run;

proc sort data=distinct;
   by _level_;
   run;
data flat;
   update distinct(obs=0) distinct;
   by _level_;
   run;
proc print;
  
run;

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

Not really summary, rather distinct values?

Without knowing the underlying requirement, it seems like an odd request.

However, I would transpose the data, and the do select distinct/proc sort nodupkey. And if it's really necessary, transpose it back..

Data never sleeps
data_null__
Jade | Level 19

This looks about right.

data have;
  input a0 a1 a2 a3 a4 a5 a6;
  datalines;
0 5 9 1 0 8 1
1 4 0 1 0 5 0
1 8 0 1 0 1 1
;;;;
   run;
proc summary data=have missing chartype;
  
class a:;
   ways 1;
  
output out=distinct(drop=_type_ _freq_) / levels;
  
run;
proc print;
  
run;

proc sort data=distinct;
   by _level_;
   run;
data flat;
   update distinct(obs=0) distinct;
   by _level_;
   run;
proc print;
  
run;

hhchenfx
Rhodochrosite | Level 12

Thank you so much, Data_null !!!
One quick question since you are here. If the name of variables shares nothing in common, say a1, f4, var1, date... I cannot use the "class a: ;" .

What should we put after "class" so that SAS conduct the proc summary for all variables on the file?

Thank you again,

HHC

Peter_C
Rhodochrosite | Level 12

something like

proc summary missing data= your.data ;

class _all_ ;

output out= results ;

run ;

data_null__
Jade | Level 19

Like says you can use _ALL_  a  SAS Variable List one of the most power features of the SAS language.

hhchenfx
Rhodochrosite | Level 12

Thank you, Peter and Data_null_.

Lesson learned.

HHC

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 2170 views
  • 6 likes
  • 4 in conversation