SAS: How To Combine Column Values Into A Single String

You can combine all the values present on one column and form a single string using macro variable. There might be a scenario where you want to create a macro variable and assign values to that variable referring to any specific variable from SAS dataset.

There are multiple ways you can create macro variables and assign values to it. But when it comes to assigning values from the entire column to the SAS macro variable, PROC SQL Select INTO: statement is the best solution.

The following sample dataset will be created and used to demonstrate how to combine column values into a single string.

/* create sample dataset */
data class;
	set sashelp.class;
run;

/* view dataset */
proc print data=work.class;
run;
sas How To Combine One column values into a single string

Combine Column Values Into A Single String

Now you can combine column “name” values into a single string using the PROC SQL procedure. The expected output would be: macro_variable= name1 name2 name3 ….

To do that you can use the Select INTO: option with PROC SQL procedure. It creates a macro variable and stores the value of one or more columns for use later in another PROC SQL query or SAS statement.

In the below example we have also used the “SEPARATED BY” delimiter that sets the rule to arrange and assign multiple values to one macro variable.

You can print the value of that macro variable using the %put statement. It writes text or macro variable information to the SAS log.

/* Combine One column values into a single string */
proc sql;
	select name into:name_string separated by " " 
	from class;
quit;

%put name_string = "&name_string";
Combine One column values into a single string in SAS