DATA Step, Macro, Functions and more

how to merge two columns into single column

Reply
Occasional Contributor
Posts: 13

how to merge two columns into single column

Hi,

I'm having a dataset shown as below and all the variables are characters. I need a output in which TERM variable is having the values of both TERM and WORD. PLease help

 

Thanks in advance.

 

INPUT:

 

TERM             WORD                   

Cold                chills                       

Fever              HIgh temperature     

Headache       dizziness                   

                                                        

                                                     

OUTPUT:

                                                        

TERM

 Cold

 chills

  fever

  high temperature

  Headache

 dizziness

PROC Star
Posts: 1,410

Re: how to merge two columns into single column

[ Edited ]

There are numerous ways to do this and it is fairly easily done, but I have to ask why? This does not make sense to me? Smiley Happy

 

data have;
length TERM $50 WORD $50;
input TERM$ WORD$;
infile datalines dlm=',';
datalines; 
Cold,chills                       
Fever,HIgh temperature     
Headache,dizziness
run; 

data want(keep=TERM);
	set have;
	array vars{*} TERM WORD;
	do i=1 to dim(vars);
		TERM=vars[i];
		output;
	end;
run;
Occasional Contributor
Posts: 13

Re: how to merge two columns into single column

its a question asked in interview.

I tried different ways but couldnt get the result.

 

will try this.

 

Occasional Contributor
Posts: 13

Re: how to merge two columns into single column

thanks a lot it worked..

Occasional Contributor
Posts: 13

Re: how to merge two columns into single column

Could you please explain how this worked out.

Trusted Advisor
Posts: 1,849

Re: how to merge two columns into single column

If you are not familiar with arrays, next code is same as @draycut's code (but his code is flexible to deal with more than 2 variables):

data want(keep=TERM);
	set have;
	output;   /* original TERM only */
        term = word;
       output;   /* = WORD value */ 
run;
Ask a Question
Discussion stats
  • 5 replies
  • 1036 views
  • 4 likes
  • 3 in conversation