SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DipeshGupta
Calcite | Level 5

I have a dataset of this format.

 

A               B              C            D            Name

Apple        Bat           Cat        100          A

Grapes     Ball           Dog        200        C

Orange     Racket      Cow       300        D

 

Now I want another column name Concatenate where first concatenation variable is the column name (NAME) and the second column

depends on the value of the column (NAME) i.e.

 

Concatenate

AApple

CDog

D300

 

Is this possible in SAS..??

1 ACCEPTED SOLUTION

Accepted Solutions
FredrikE
Rhodochrosite | Level 12

I really like the function VVALUEX(var) which returns the value of the variable specified in the variable var, ex. in your case:

 

new = cats(name,vvaluex(name));

 

//Fredrik

View solution in original post

7 REPLIES 7
Jim_G
Pyrite | Level 9

data;   set;

if name='A' then concat=catt(name,a);

if name='B' then concat=catt(name,b);

if name='C' then concat=catt(name,c);

if name='D' then concat=catt(name,d);

 

 

Jim

DipeshGupta
Calcite | Level 5

Actually the thing is columns are not fixed and I want to find the column to be concatenated depending on the value taken by column NAME.

 

A               B              C            D            Name

Apple        Bat           Cat        100          A

Grapes     Ball           Dog        200        C

Orange     Racket      Cow       300        D

 

 

 

Concatenate

AApple

CDog

D300

 

 

So when the value is A then take the value of column A and if it changes to D then it automatically takes the value of D and concatenate the column NAME with column D.

 

The main problem is columns are not fixed.

 

Jim_G
Pyrite | Level 9

data; input a$ b$ c$ d name $;
cards;
Apple  Bat       Cat 100 A
Grapes Ball    Dog       200  C
Orange Racket Cow 300         D
proc print; run;
data; set;
if name='A' then concat=catt(name,a);
if name='B' then concat=catt(name,b);
if name='C' then concat=catt(name,c);
if name='D' then concat=catt(name,d);

 

proc print; run;

 

This works for me.   columns are not fixed.  Jim

Ksharp
Super User
data have;
input (A               B              C            D            Name) ($);
want=cats(name,vvaluex(name));
cards;
Apple        Bat           Cat        100          A
Grapes     Ball           Dog        200        C
Orange     Racket      Cow       300        D
;
run;


Astounding
PROC Star

It's easier if you can assume that you want to work with all character variables except NAME.  If that's not the case, you will need a way to come up with all the proper variable names:

 

data want;

if 5=4 then do;

   set have (drop=name);

   array chars {*} _character_;

end;

set have;

length concatenate $ 40;   

do _n_=1 to dim(chars) until (concatenate > ' ');

   if name = vname(chars{_n_}) then 

   concatenate = catt(name, chars{_n_});

end;

run;

 

It's untested, so might need a small amount of debugging.

 

That being said, I like what KSharp did (but wonder what would happen if NAME is incorrect and doesn't actually match anything).

FredrikE
Rhodochrosite | Level 12

I really like the function VVALUEX(var) which returns the value of the variable specified in the variable var, ex. in your case:

 

new = cats(name,vvaluex(name));

 

//Fredrik

DipeshGupta
Calcite | Level 5

Tkank You So Much. 🙂

 

It worked prefectly.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 7 replies
  • 10395 views
  • 3 likes
  • 5 in conversation