Desktop productivity for business analysts and programmers

Do Loops or Data Step?

Accepted Solution Solved
Reply
Frequent Learner
Posts: 1
Accepted Solution

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

ADD            6          C

 

Results desired;

SomeID          new field

ABC               1,Z;2,X

ACC               3,X

ADD               6,C

 

Thanks,

 

SAS EG 7.13

 


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

Re: Do Loops or Data Step?

Posted in reply to novinosrin

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

 

View solution in original post


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
ADD            6          C
;

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?

Posted in reply to novinosrin

@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 Smiley Sad

Super User
Posts: 13,293

Re: Do Loops or Data Step?

Posted in reply to novinosrin

@novinosrin wrote:

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


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 Smiley Happy

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.

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

Discussion stats
  • 6 replies
  • 162 views
  • 1 like
  • 3 in conversation