Solved
Frequent Learner
Posts: 1

# Do Loops or Data Step?

have a source dataset that I need to get a column of distinct values and then "pivot" out the associated columns into a single new field.

Data source;

SomeID        Acct   Prod

ABC            1          Z

ABC           2           X

ACC           3           X

Results desired;

SomeID          new field

ABC               1,Z;2,X

ACC               3,X

Thanks,

SAS EG 7.13

Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 13,293

## Re: Do Loops or Data Step?

@novinosrin

OP shows semicolons between the "row groups"

```data want;
set have;
by someid;
length new \$50;
retain new;
if first.someid then new=catx(',',acct,prod);
else new=catx(';',new,catx(',',acct,prod));
if last.someid;
run;```

And of course NEW may need to have a length defined to hold much more text than the example data provides.

All Replies
PROC Star
Posts: 1,559

## Re: Do Loops or Data Step?

I hope this helps

``````data have;
input SomeID   \$     Acct   Prod \$;
cards;
ABC            1          Z
ABC           2           X
ACC           3           X
;

data want;
set have;
by someid;
length new \$50;
retain new;
if first.someid then new=catx(',',acct,prod);
else new=catx(',',new,acct,prod);
if last.someid;
run;``````
Solution
4 weeks ago
Super User
Posts: 13,293

## Re: Do Loops or Data Step?

@novinosrin

OP shows semicolons between the "row groups"

```data want;
set have;
by someid;
length new \$50;
retain new;
if first.someid then new=catx(',',acct,prod);
else new=catx(';',new,catx(',',acct,prod));
if last.someid;
run;```

And of course NEW may need to have a length defined to hold much more text than the example data provides.

PROC Star
Posts: 1,559

## Re: Do Loops or Data Step?

@ballardw Thank you sir. I really need to get my eyes tested. Time to wear glasses

Super User
Posts: 13,293

## Re: Do Loops or Data Step?

@novinosrin wrote:

@ballardw Thank you sir. I really need to get my eyes tested. Time to wear glasses

I was trying to figure out just what this could be useful for so was looking for different delimiters.

PROC Star
Posts: 1,559

## Re: Do Loops or Data Step?

LOL Believe it or not, This isn't the first time that you brilliantly pointed out every bits of detail to correct me and I can't appreciate enough. I even told that to my parents(my mom in particular who taught me how to program using hash objects) and she opined "not to throw up codes when you are hung over, pay attention to every detail like @ballardw does"

There you go

Super User
Posts: 13,293

## Re: Do Loops or Data Step?

Can you describe exactly how that new field will be used in other processes? If you are doing much other  than printing the result things get real ugly real fast.

☑ This topic is solved.