Desktop productivity for business analysts and programmers

Concatenate multiple rows into one (different operations for different columns)

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

Concatenate multiple rows into one (different operations for different columns)

[ Edited ]

Please use the following codes to generate data:

options validvarname=any;

/* user defined */
%let IDList = ID;
%let SumValueList = Balance;
%let CharList = Gender Date Account;

/* dataset - have */
data have;
	input ID Gender $ Date ddmmyy10. Balance Account Nonlist;
cards;
1 M 01/01/1990 . 2 9
1 M 01/05/1991 0 2 8
1 F 31/12/2000 800 5 10
;
run;

/* dataset - want */
data want;
	input ID Gender $ Date $char32. Balance Account $ Nonlist;
cards;
1 M,F 01/01/1990,01/05/1991,31/12/2000 800 2,5 9
;
run;

If the column name is listed ...

  • in the 'IDList' macro variable: such column used to identify similar rows (that need to be concatenated).
  • in the 'SumValueList': sum up all numeric values in such columns. In our example it would be 800 = . + 0 + 800. Please note:
    • missing values '.' treated as 0 
    • only sum up unique values: e.g. 100 80 . 100 would be 180 (100 + 80 + .) rather than 280
  • in the 'CharList': concatenate all texts, separated by ',' . Please note:
    • the length of the columns is very likely to be changed in order to capture all information.
    • only capture unique values: e.g. 2 2 5 would be 2,5 rather than 2,2,5
  • Not listed in any of the macro variables above, use the value that appears first.

How to achieve it? I've seen other posts with similar problems, but in our case, different columns require different operations.

Thanks.


Accepted Solutions
Solution
‎04-26-2017 01:05 AM
Respected Advisor
Posts: 4,812

Re: Concatenate multiple rows into one (different operations for different columns)

This does it:

 

options validvarname=any;

/* user defined */
%let IDList = ID;
%let SumValueList = Balance;
%let CharList = Gender Date Account;

/* dataset - have */
data have;
	input ID Gender $ Date ddmmyy10. Balance Account Nonlist;
    format date ddmmyy10.; /* <-- Added */
cards;
1 M 01/01/1990 . 2 9
1 M 01/05/1991 0 2 8
1 F 31/12/2000 800 5 10
;

proc sort data=have; by &idlist; run;

%let id=%scan(&idlist,-1);

%macro one;
data 
    v(keep=&idlist var value) 
    c(keep=&idlist var txt) 
    f(drop=&sumvaluelist &charlist value var txt i);
array v{*} &sumvaluelist;
length var txt $32;
set have; by &idlist;
if first.&id then output f;
do i = 1 to dim(v);
    value = v{i};
    var = vname(v{i});
    output v;
    end;
%let n=1;
%do %while(%scan(&charlist,&n) ne );
    txt = vvalue(%scan(&charlist,&n));
    var = "%scan(&charlist,&n)";
    output c;
    %let n=%eval(&n+1);
    %end;
run;
%mend one;
%one;

proc sort data=v nodupkey; by &idlist var value; run;
proc sort data=c nodupkey; by &idlist var txt; run;

data vl;
listSum = 0;
do until(last.var);
    set v; by &idlist var;
    listSum =  sum(listSum, value);
    end;
drop value;
run;

proc transpose data=vl out=vt(drop=_name_);
by &idlist;
id var;
var listSum;
run;

data cl;
length listTxt $200;
do until (last.var);
    set c; by &idlist var;
    listTxt = catx(",", listTxt, txt);
    end;
drop txt;
run;

proc transpose data=cl out=ct(drop=_name_);
by &idlist;
id var;
var listTxt;
run;

data want;
merge f vt ct;
by &idlist;
run;
PG

View solution in original post


All Replies
Super User
Posts: 19,064

Re: Concatenate multiple rows into one (different operations for different columns)

This isn't coming across as a request for help but to code a fully functional solution to a question. Which you've asked before but didn't like the answers. At this point please consider simplifying your question to which component you're having issues with at the moment or consider hiring outside help. 

Frequent Contributor
Posts: 80

Re: Concatenate multiple rows into one (different operations for different columns)

Hi Reeza, thank you for commenting on it.

 

Sorry I was unable to simplify the question further due to a limit of SAS knowledge. Was planning to break it down to maybe three smaller questions and post separately, but realized other people with similar questions might benefit from this post without doing additional research.

 

After all, SAS Communities is a great place where SAS users can learn a lot because of you guys. Smiley Happy

As long as it is not a big/complex project, maybe most companies are inclined to let their SAS users do their research.

Solution
‎04-26-2017 01:05 AM
Respected Advisor
Posts: 4,812

Re: Concatenate multiple rows into one (different operations for different columns)

This does it:

 

options validvarname=any;

/* user defined */
%let IDList = ID;
%let SumValueList = Balance;
%let CharList = Gender Date Account;

/* dataset - have */
data have;
	input ID Gender $ Date ddmmyy10. Balance Account Nonlist;
    format date ddmmyy10.; /* <-- Added */
cards;
1 M 01/01/1990 . 2 9
1 M 01/05/1991 0 2 8
1 F 31/12/2000 800 5 10
;

proc sort data=have; by &idlist; run;

%let id=%scan(&idlist,-1);

%macro one;
data 
    v(keep=&idlist var value) 
    c(keep=&idlist var txt) 
    f(drop=&sumvaluelist &charlist value var txt i);
array v{*} &sumvaluelist;
length var txt $32;
set have; by &idlist;
if first.&id then output f;
do i = 1 to dim(v);
    value = v{i};
    var = vname(v{i});
    output v;
    end;
%let n=1;
%do %while(%scan(&charlist,&n) ne );
    txt = vvalue(%scan(&charlist,&n));
    var = "%scan(&charlist,&n)";
    output c;
    %let n=%eval(&n+1);
    %end;
run;
%mend one;
%one;

proc sort data=v nodupkey; by &idlist var value; run;
proc sort data=c nodupkey; by &idlist var txt; run;

data vl;
listSum = 0;
do until(last.var);
    set v; by &idlist var;
    listSum =  sum(listSum, value);
    end;
drop value;
run;

proc transpose data=vl out=vt(drop=_name_);
by &idlist;
id var;
var listSum;
run;

data cl;
length listTxt $200;
do until (last.var);
    set c; by &idlist var;
    listTxt = catx(",", listTxt, txt);
    end;
drop txt;
run;

proc transpose data=cl out=ct(drop=_name_);
by &idlist;
id var;
var listTxt;
run;

data want;
merge f vt ct;
by &idlist;
run;
PG
Frequent Contributor
Posts: 80

Re: Concatenate multiple rows into one (different operations for different columns)

Thank you so much PG!
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 256 views
  • 1 like
  • 3 in conversation