BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayin
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
Reeza
Super User

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. 

ayin
Quartz | Level 8

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.

PGStats
Opal | Level 21

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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