Concatenate Two Columns in SAS

In order to concatenate two columns in SAS we will be using CAT Function. Concatenate two columns with space in SAS. Concatenate two columns with hyphen. Concatenate two character columns in SAS. Concatenate a character and numeric column in SAS

  • Concatenate two columns in SAS without removing space
  • Concatenate two columns in SAS with single space
  • Concatenate two columns in SAS with hyphen
  • Concatenate two columns in SAS with removing space
  • Concatenate character and numeric column in SAS
  • Concatenate two character columns in SAS

So we will be using EMP_DET Table in our example

Concatenate Two Columns in SAS 1

 

 

Concatenate two Columns in SAS – Without Removing Space

Concatenate two columns in SAS without removing space using CAT() Function.




/** simply concatenate without removing space **/

data EMP_DET1;
set EMP_DET;
state = CAT(state_name,state_code);
run;

So the resultant table with two columns concatenated will be
Concatenate Two Columns in SAS 2

 

 

Concatenate two Columns in SAS – With Single Space

Concatenate two columns in SAS with single space using CATX() Function. CATX() Function takes column names along with space as argument.


/*** concatenate with single space **/ 

data EMP_DET1; 
set EMP_DET; 
state = CATX(' ' , state_name,state_code); 
run;


So the resultant table with two columns concatenated with single space will be
Concatenate Two Columns in SAS 3

 

 

Concatenate two Columns in SAS – With hyphen

Concatenate two columns in SAS with hyphen using CATX() Function. CATX() Function takes column names along with hyphen(“-”) as argument.



/** concatenate with single hyphen **/

data EMP_DET1; 
set EMP_DET; 
state = CATX('-',state_name,state_code); 
run; 

So the resultant table with two columns concatenated with hyphen will be
Concatenate Two Columns in SAS 4

 

 

Concatenate two Columns in SAS – With Removing Space

Concatenate two columns in SAS by removing space using CATS() Function. CATS() Function takes column names as argument and removes all the spaces.


/*** concatenate by removing leading ,trailing and all space ***/ 
 
data EMP_DET1; 
set EMP_DET; 
state = CATS(state_name,state_code); 
run;

So the resultant table with all the spaces removed will be
Concatenate Two Columns in SAS 5

 

 

Concatenate two Columns in SAS – Concatenate Numeric and character column

Concatenate numeric and character columns in SAS using CATX() Function. CATX() Function takes column names as argument and concatenate them


/** concatenate numeric and character column **/ 

data EMP_DET1; 
set EMP_DET; 
state = CATX('',state_name,District); 
run;


So the resultant table with numeric and character column concatenated will beConcatenate Two Columns in SAS 6

 

                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts