I have the following:
data have;
infile datalines dlm=',' dsd;
input fruit:$32. veg:$32. num:32.
;
datalines;
apple, potato, 30
banana, celery, 50
berry, zucchini, 40
;
run;
How can I append all the columns into a single one so that I get something like this? (I'm aware one column is numeric and would be converted to char)
var
apple
banana
berry
potato
celery
zucchini
30
50
40
data have;
infile datalines dlm=',' dsd;
input fruit:$32. veg:$32. num:32.
;
datalines;
apple, potato, 30
banana, celery, 50
berry, zucchini, 40
;
run;
proc transpose data=have out=w(keep=col1);
by _all_;
var _all_;
run;
/*for alignement correction*/
data want;
set w;
want=left(col1);
drop col1;
run;
EDIT: The above will produce a auto-conversion note in the log
/*cleaner method*/
data _have;
set have;
char=put(num,8. -l);
drop num;
run;
proc transpose data=_have out=want(keep= col1);
by _all_;
var _all_;
run;
@Datino I like this as best imho
data want;
set have;
temp=catx(' ',fruit,veg,num);
do _n_=1 to countw(temp,' ');
want=scan(temp,_n_,' ');
output;
end;
keep want;
run;
Make sure to add a length statement for temp like
Length temo $50;
at the top
Is the output order important, critical or not?
Not important at all.
Thank you @ballardw,, you are right to make meaningful grouping as-->
data _null_;
set have end=lr;
if _n_=1 then do;
dcl hash H (ordered: "A", multidata:'y') ;
h.definekey ("vn") ;
h.definedata ("vn","want") ;
h.definedone () ;
end;
array t(*) fruit veg;
do i= 1 to 2;
vn=vname(t(I));
want=t(I);
h.add();
end;
want=put(num,8. -l);
vn='num';
h.add();
if lr then h.output(dataset:'want');
run;
data have;
infile datalines dlm=',' dsd;
input fruit:$32. veg:$32. num:32.
;
datalines;
apple, potato, 30
banana, celery, 50
berry, zucchini, 40
;
run;
proc sql;
create table want as
select fruit from have
union all
select veg from have
union all
select put(num,best. -l) from have;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.