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-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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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