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?

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