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

Hi,

data have;
length name1 name2 name3 name4 $20;
name1='B'; name2= ''; name3= 'A'; name4='A';
output;
name1=' '; name2= 'B'; name3= ''; name4='';
output;
name1='A'; name2= 'A'; name3= 'A'; name4='';
output;
run;

 

Want output as

row DistinctName DistinctN
1    A, B                      2
2    B                          1
3    A                          1

 

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Not pretty or elegant but should be robust and scalable if you actually have more variables:

data have;
length name1 name2 name3 name4 $20;
input  name1 name2 name3 name4;
row+1;
datalines;
B .  A A
. B .  .
A A A .
;

proc transpose data=have out=trans (where=( not missing(col1)) drop=_name_);
by row;
var name1-name4;
run;
proc sql;
   create table temp as
   select distinct row,col1
   from trans;
quit;
data want;
   set temp;
   by row;
   /* need to set length of a target to string
      to at least longest possible so 20*number of variables+ number of variables
      minus 1 for commas.
   */
   length DistinctName $85; 
   retain DistinctName;
   if first.row then DistinctName=col1;
   else DistinctName= catx(',',DistinctName,col1);
   distinctn= countw(DistinctName,',');
   if last.row;
   drop col1;
run;

Time to learn how to write a data step. That list of variables and output statements gets cumbersome quickly.

Also use the code box on the forum opened with the {I} or "running man" icons for posting code.

 

And just for curiosity how will that comma delimited list of "names" actually be used?

View solution in original post

3 REPLIES 3
jarg
Fluorite | Level 6

is this what you're after?

 

proc sql;
create table cat_count as
select cats(name1,name2,name3,name4) as ConcatenatedString
,count(*) as Volume
from have
group by ConcatenatedString
order by ConcatenatedString;
quit;

Ram_SAS
Obsidian | Level 7
No. you are basically concatenating and counting. Instead, the concatenated string should have unique sorted string separated by comma (e.g. A,B or A) and the distinct N should count distinct strings (e.g. 2 or 1).
ballardw
Super User

Not pretty or elegant but should be robust and scalable if you actually have more variables:

data have;
length name1 name2 name3 name4 $20;
input  name1 name2 name3 name4;
row+1;
datalines;
B .  A A
. B .  .
A A A .
;

proc transpose data=have out=trans (where=( not missing(col1)) drop=_name_);
by row;
var name1-name4;
run;
proc sql;
   create table temp as
   select distinct row,col1
   from trans;
quit;
data want;
   set temp;
   by row;
   /* need to set length of a target to string
      to at least longest possible so 20*number of variables+ number of variables
      minus 1 for commas.
   */
   length DistinctName $85; 
   retain DistinctName;
   if first.row then DistinctName=col1;
   else DistinctName= catx(',',DistinctName,col1);
   distinctn= countw(DistinctName,',');
   if last.row;
   drop col1;
run;

Time to learn how to write a data step. That list of variables and output statements gets cumbersome quickly.

Also use the code box on the forum opened with the {I} or "running man" icons for posting code.

 

And just for curiosity how will that comma delimited list of "names" actually be used?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 3015 views
  • 0 likes
  • 3 in conversation