Contributor
Posts: 32

# SAS code for creating value from two columns with recurring values

Hi All,

I am trying to create a column that will have the entries that combines two other columns. For example I want to create column C where value will include from Column A and Column B such that:

Column A                  Column B                           Column C

--------------               -----------------                      -----------------

12 Kilometers           A1001                               A1001-X3

6 Kilometers            A1001                                A1001-X2

3 Kilometer              A1001                               A1001-X1

12 Kilometers          A1002                               A1002-X3

6 Kilometers            A1002                               A1002-X2

3 Kilometers            A1002                               A1002-X1

12 Kilometers         A1003                                A1003-X3

6 Kilometers           A1003                                A1003-X2

3 Kilometers           A1003                                A1003-X1

and so on               and so on                          and so on

and so on               and so on                          and so on

For the first three, I made SAS code as,

data x; set y;

if column A= '12 Kilometers' and column B= 'A1001' then Column C= 'A1001-X3';

if column B= '6 Kilometers' and column B= 'A1001' then Column C= 'A1001-X2';

if column A= '3 Kilometers' and column B= 'A1001' then Column C = 'A1001-X1';

run;

My question is:

How do I all create similar values in Column C for the rest of the numbers (i.e. A1002, A1003, A1004, A1005....) in one or two steps without having to

write code for each and every number? If there is a simple, 1-2 generic steps that take care of the all the numbers that I want to put in column C?

Thanks in advance.

Sapkota

Super Contributor
Posts: 578

## Re: SAS code for creating value from two columns with recurring values

if column A= '12 Kilometers' and column B= 'A1001' then Column C= cats(ColumnB, '-X3');

else if column B= '6 Kilometers' and column B= 'A1001' then Column C= cats(ColumnB, '-X2');

else if column A= '3 Kilometers' and column B= 'A1001' then Column C = cats(ColumnB, '-X1');

Contributor
Posts: 32

Thank you much.

PROC Star
Posts: 8,169

## Re: SAS code for creating value from two columns with recurring values

You could just create and apply a format.  E.g.:

proc format;

value \$colc

'12 Kilometers'='X3'

'6 Kilometers'='X2'

'3 Kilometers'='X1'

;

run;

data want;

set have;

ColumnC=catx('-',ColumnB,put(ColumnA,\$colc.));

run;

Contributor
Posts: 32

## Re: SAS code for creating value from two columns with recurring values

Thanks Arthur,

Your advise worked for me. I got my answer!

Discussion stats
• 4 replies
• 252 views
• 2 likes
• 3 in conversation