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 ...
How to achieve it? I've seen other posts with similar problems, but in our case, different columns require different operations.
Thanks.
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;
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.
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.
As long as it is not a big/complex project, maybe most companies are inclined to let their SAS users do their research.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.