BookmarkSubscribeRSS Feed
Guptashwe
Calcite | Level 5

how can we print the expected output using characters function

 

DATA TEST;
INPUT DEPARTMENT $ PRODUCTS $;
DATALINES;
D1 X1
D1 X2
D2 Y1
D2 Y2
D2 Y3
D3 Z1
D3 Z2
D3 Z3
D3 Z4
;
RUN;

 

expected output

Department Products

D1               X1,X2

D2               Y1,Y2,Y3

D3               Z1,Z2,Z3,Z4

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16
DATA TEST;
INPUT DEPARTMENT $ PRODUCTS $;
DATALINES;
D1 X1
D1 X2
D2 Y1
D2 Y2
D2 Y3
D3 Z1
D3 Z2
D3 Z3
D3 Z4
;
RUN;

data want;
length PRODUCTS $100.; set test(rename=(PRODUCTS=_PRODUCTS)); by DEPARTMENT _PRODUCTS; retain PRODUCTS; if first.DEPARTMENT then PRODUCTS=_PRODUCTS; else PRODUCTS=catx(',',PRODUCTS,_PRODUCTS); if last.DEPARTMENT; drop _PRODUCTS; run;
Thanks,
Jag
r_behata
Barite | Level 11
DATA TEST;
INPUT DEPARTMENT $ PRODUCTS $;
DATALINES;
D1 X1
D1 X2
D2 Y1
D2 Y2
D2 Y3
D3 Z1
D3 Z2
D3 Z3
D3 Z4
;
RUN;

data Want(rename=(products_=products));
if _n_=0 then do;
	set test;
end;
Length products_ $12.;
products_=' ';
do until(last.DEPARTMENT);
set test;
	by department;
	products_=catx(',',products_,products);
end;

drop products;
run;
novinosrin
Tourmaline | Level 20

DATA TEST;
INPUT DEPARTMENT $ PRODUCTS $;
DATALINES;
D1 X1
D1 X2
D2 Y1
D2 Y2
D2 Y3
D3 Z1
D3 Z2
D3 Z3
D3 Z4
;
RUN;

proc transpose data=test out=t(drop=_name_);
by department;
var products;
run;

data want;
set t;
length products $50;
products=catx(',',of col:);
drop col:;
run;
mkeintz
PROC Star

If all you really want to do is PRINT the data, then a single DATA _NULL_ step works:

 

DATA TEST;
INPUT DEPARTMENT $ PRODUCTS $;
DATALINES;
D1 X1
D1 X2
D2 Y1
D2 Y2
D2 Y3
D3 Z1
D3 Z2
D3 Z3
D3 Z4
run;
data _null_;
  file print;
  set test;
  by department;
  if first.department then put / department  @5 products @;
  else put +(-1) ',' products @;
run;

 

  1. The PUT /   says to advance a line.
  2. The trailing @ in a put statement tells SAS to hold the column pointer. So the next PUT statement will not automatically start a new line.  Instead it write to the location of the pointer.
  3. The "else put +(-1)" ordinarily uses the + sign to move the column pointer forward.  But in this case it's actually "+(-1)" which tells sas to move BACK one column.  That's necessary because the trailing @ after a variable name (as opposed to a character literal) actually advances one blank prior to holding the pointer.  This overwrites that blank.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 4 replies
  • 1172 views
  • 1 like
  • 5 in conversation