BookmarkSubscribeRSS Feed
Sapkota
Calcite | Level 5

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

4 REPLIES 4
DBailey
Lapis Lazuli | Level 10

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');

art297
Opal | Level 21

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;

Sapkota
Calcite | Level 5

Thanks Arthur,

Your advise worked for me. I got my answer!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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