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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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