BookmarkSubscribeRSS Feed
Datino
Obsidian | Level 7

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

11 REPLIES 11
novinosrin
Tourmaline | Level 20

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  

novinosrin
Tourmaline | Level 20
/*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;
novinosrin
Tourmaline | Level 20

@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 

ballardw
Super User

Is the output order important, critical or not?

 

Datino
Obsidian | Level 7

Not important at all.

novinosrin
Tourmaline | Level 20

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;
Peter_C
Rhodochrosite | Level 12
Data onecolumn ;
Length item $50 ;
INFILE "your data" TRUNCOVER lrecl= 32760 dad;
Input item @@;
Run;
Peter_C
Rhodochrosite | Level 12
D*** autocorrect
For DAD read DSD
Peter_C
Rhodochrosite | Level 12
More code could be added :
If the column number is needed
If numeric validation is needed for column3
If columns might be empty
If columns after the third should be ignored

However, it shouldn't need much more than the code above
(might need to use FLOWOVER rather than TRUNCOVER)
Ksharp
Super User
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;
Peter_C
Rhodochrosite | Level 12
Would this work as a single pass of WANT if the data step created it as a VIEW?

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 11 replies
  • 1801 views
  • 0 likes
  • 5 in conversation