Code Generator | SAS

merge_columns Function

The merge_columns function allows users to merge two columns from a specified dataset into a new column. This function takes in the parameters dataset (name of the dataset containing the columns to be merged), column1 (name of the first column to


Empty image or helper icon

Prompt

i need to merge two columns

Answer

/*
Function: merge_columns
Parameters: 
- dataset: the name of the dataset containing the columns to be merged
- column1: the name of the first column to be merged
- column2: the name of the second column to be merged
- new_column: the name of the new column to be created from the merged values of column1 and column2
*/
data merge_columns (keep = new_column);
    set dataset;
    new_column = cats(column1, column2);
run;

Example Usage:

data mydata;
    input column1 $ column2 $;
    datalines;
    John Doe
    Jane Smith
    ;
run;

data merged_data;
    set merge_columns(dataset = mydata, column1 = column1, column2 = column2, new_column = merged);
run;

In the above example, we have a dataset mydata with two columns column1 and column2. We want to merge the values of these two columns into a new column named merged. By using the merge_columns function, we can easily achieve this by specifying the dataset name, column names, and the new column name. The resulting dataset merged_data will have all the original columns from mydata along with the newly created merged column.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

The merge_columns function allows users to merge two columns from a specified dataset into a new column. This function takes in the parameters dataset (name of the dataset containing the columns to be merged), column1 (name of the first column to be merged), column2 (name of the second column to be merged), and new_column (name of the new column to be created from the merged values of column1 and column2). By using this function, users can easily merge columns and create a new dataset with the merged column.