BookmarkSubscribeRSS Feed
altijani
Quartz | Level 8

Greetings Community,

 

I have the following data:

 

CustomerItem1Item2Item3
JohnShoes  
DavidBagsShirts 
NatashaShoesShirtsBags

 

I need to concatenate the items in one var as follows. Please note for some customers it can go up to 10 items:

 

CustomerItems
JohnShoes
DavidBags, Shirts
NatashaShoes, Shirts, Bags

 

Thank you

8 REPLIES 8
sidpesar
Obsidian | Level 7

proc sql;

 create table want as select customer , cats(col1,col2,col3..) as concat_items from have;

quit;

 

to automate:

proc sql noprint;
select name
into :name_col separated by ','
from dictionary.columns
where upcase(memname) = 'have'
and upcase(name) like 'item%' ;
quit ;

proc sql;
create table want as select customer , cats(&name_col.) as concat_items from have;
quit;

novinosrin
Tourmaline | Level 20
data have;
infile cards truncover;
input Customer :$10.	(Item1	Item2	Item3) ($);
cards;
John	Shoes	 	 
David	Bags	Shirts	 
Natasha	Shoes	Shirts	Bags
;

data want;
set have;
length concat $50;
concat=catx(',',of item:);
run;

 

Proc sql does not support variable lists, nor array processing etc. These kind of operations are best performed in a datastep, plus I think you need catx instead of cats with delim ',' in between 

ChrisHemedinger
Community Manager

See: How to concatenate values in SAS.

 

You should be able to do something like:

 

  newValue = catx(', ', of item:);
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
altijani
Quartz | Level 8

Thank you for your suggested code.
I tried these, and my issue the trailing commas.

In case of Natasha, all is fine.
In case of David, there are 2 trailing commas
In case of Smith, there is a 1 trailing comma

Got my point?


That what I would like to solve.

ballardw
Super User

@altijani wrote:

Thank you for your suggested code.
I tried these, and my issue the trailing commas.

In case of Natasha, all is fine.
In case of David, there are 2 trailing commas
In case of Smith, there is a 1 trailing comma

Got my point?


That what I would like to solve.


Show the code you used. There were several solutions suggested and without the actual code you used it is hard to recommend additional pieces.

altijani
Quartz | Level 8

Here is the code I am trying to use:

 

proc sql;

CREATE TABLE WANT AS

SELECT DISTINCT

Customer,

Item1,

Item2,

Item3,

 

(CATX (" ", Item1, ", ", Item2, ", ", Item3)) AS Items

FROM Have;

 

novinosrin
Tourmaline | Level 20

data have;
infile cards truncover;
input Customer :$10.	(Item1	Item2	Item3) ($);
cards;
John	Shoes	 	 
David	Bags	Shirts	 
Natasha	Shoes	Shirts	Bags
;
data want;
set have;
array t(*) $20 item:;
length concat $50;
concat = peekc(addr (t[1]),20);
run;
V_27
Obsidian | Level 7

 

data want;
   set have;
   Item_combined=strip(tranwrd(catx(", ",Item1, Item2, Item3),"",""));
   drop Item1-Item3;
run;

 

 

Hi @altijani,

 

Try this sas code.

 

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
  • 8 replies
  • 2472 views
  • 1 like
  • 6 in conversation