Desktop productivity for business analysts and programmers

SAS Concatenate Function

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

SAS Concatenate Function

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..??


Accepted Solutions
Solution
‎09-02-2017 01:05 AM
Regular Contributor
Posts: 194

Re: SAS Concatenate Function

Posted in reply to DipeshGupta

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


All Replies
Frequent Contributor
Posts: 99

Re: SAS Concatenate Function

Posted in reply to DipeshGupta

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

Contributor
Posts: 70

Re: SAS Concatenate Function

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.

 

Frequent Contributor
Posts: 99

Re: SAS Concatenate Function

[ Edited ]
Posted in reply to DipeshGupta

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

Super User
Posts: 10,123

Re: SAS Concatenate Function

Posted in reply to DipeshGupta

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;


Super User
Posts: 5,610

Re: SAS Concatenate Function

[ Edited ]
Posted in reply to DipeshGupta

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).

Solution
‎09-02-2017 01:05 AM
Regular Contributor
Posts: 194

Re: SAS Concatenate Function

Posted in reply to DipeshGupta

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

Contributor
Posts: 70

Re: SAS Concatenate Function

Tkank You So Much. Smiley Happy

 

It worked prefectly.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 566 views
  • 3 likes
  • 5 in conversation