I just want an extended version of this output with two more variables. Let's say I have the dataset like the below.
data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
Is it possible to get an output like the below. There is one more variable which has more than one value for one Name and that the order is also changed.
Name1
RD|10
RS|1|2
AS
GS
Name2
RD|20
RS|3|4
RS|5|6
RS|7|8
AS
TS
TT
ST
Name3
RD|30
RS|11|12
RS|13|14
AS
GS
HG
Name4
RD|40
RS|15|16
AS
GS
Thanks in advance!
Well, this is really ugly code.
data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;
data _null_;
set have;
by F1;
file "&Benutzer\narf.txt";
length line buffer3 buffer5 $ 200;
retain buffer:;
array buffs[2] buffer3 buffer5;
if first.F1 then do;
call missing(of buffer:);
put F1;
line = catx('|', F2, F2_1);
put line;
end;
line = catx('|', of F4:);
put line;
if not find(buffer3, F3) then do;
buffer3 = catx(' ', buffer3, F3);
end;
if not find(buffer5, F5) then do;
buffer5 = catx(' ', buffer5, F5);
end;
if last.F1 then do;
do b = 1 to dim(buffs);
do i = 1 to countw(buffs[b]);
line = scan(buffs[b], i, ' ');
put line;
end;
end;
end;
run;
Can you describe to transforming logic?
Are F2 and F2_1 always the same for one F1?
Yes F2 and F2_1 will have only one value for one F1 Value. Only F4 F4_1 F4_2 and F5 will have more than one value for one F1.
Something like below might do:
data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;
data _null_;
if _n_=1 then
do;
length order 8 String $32;
dcl hash h1(ordered:'y');
h1.defineKey('F1','order','String');
h1.defineData('String');
h1.defineDone();
call missing(of _all_);
end;
set have end=last;
order=1; String=F1;
h1.ref();
order=2; String=catx('|',of F2:);
h1.ref();
order=3; String=catx('|',of F4:);
h1.ref();
order=4; String=F3;
h1.ref();
order=5; String=F5;
h1.ref();
if last then h1.output(dataset:'want(keep=String)');
run;
data _null_;
file print;
set want;
put String;
run;
Thanks much!!!
Well, this is really ugly code.
data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;
data _null_;
set have;
by F1;
file "&Benutzer\narf.txt";
length line buffer3 buffer5 $ 200;
retain buffer:;
array buffs[2] buffer3 buffer5;
if first.F1 then do;
call missing(of buffer:);
put F1;
line = catx('|', F2, F2_1);
put line;
end;
line = catx('|', of F4:);
put line;
if not find(buffer3, F3) then do;
buffer3 = catx(' ', buffer3, F3);
end;
if not find(buffer5, F5) then do;
buffer5 = catx(' ', buffer5, F5);
end;
if last.F1 then do;
do b = 1 to dim(buffs);
do i = 1 to countw(buffs[b]);
line = scan(buffs[b], i, ' ');
put line;
end;
end;
end;
run;
How about this one ?
data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
filename x temp;
data _null_;
file x;
length x y $ 20000;
do i=1 by 1 until(last.f2_1);
set have;
by f1 f2 f2_1;
x= catx(',',x,catx('|',F4,F4_1,F4_2));
if i=1 then y=catx(',',y,F3,F5);
else y=catx(',',y,F5);
end;
put F1;
put F2 +(-1) '|' F2_1;
do i=1 to countw(x,',');
temp=scan(x,i,',');
put temp;
end;
do i=1 to countw(y,',');
temp=scan(y,i,',');
put temp;
end;
run;
data _null_;
infile x;
input;
put _infile_;
run;
Thank you!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.