SAS Proc SQL——如何在SAS中执行Listagg函数
下午好 我正在使用Proc SQL在SAS上寻找Listag函数 比如说SAS Proc SQL——如何在SAS中执行Listagg函数,sas,proc-sql,Sas,Proc Sql,下午好 我正在使用Proc SQL在SAS上寻找Listag函数 比如说 id product_name 1001 Bananas 1002 Bananas 1002 Apples 1002 Peach 1003 Pears proc sql; create table work.test2 as select id, _____(',', product_name) from test1 group by
id product_name
1001 Bananas
1002 Bananas
1002 Apples
1002 Peach
1003 Pears
proc sql;
create table work.test2 as
select id, _____(',', product_name)
from test1
group by id
order by 1;
quit;
结果
id product_name
1001 Bananas
1002 Bananas,Apples,Peach
1003 Pears
SAS中有类似的功能?您可以执行
data have;
input id product_name $;
datalines;
1001 Bananas
1002 Bananas
1002 Apples
1002 Peach
1003 Pears
;
data want(rename=(product=product_name));
do until(last.id);
set have;
by id;
length product $50.;
product =catx(',',product_name, product);
end;
drop product_name;
run;
你能行
data have;
input id product_name $;
datalines;
1001 Bananas
1002 Bananas
1002 Apples
1002 Peach
1003 Pears
;
data want(rename=(product=product_name));
do until(last.id);
set have;
by id;
length product $50.;
product =catx(',',product_name, product);
end;
drop product_name;
run;
下面是解决此类问题的两种方法的示例:
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
3 football CA
;
run;
*Sort - required for both options;
proc sort data=have;
by orgID;
run;
**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
set have;
by orgID;
length combined $100.;
retain combined;
if first.orgID then
combined=states;
else
combined=catx(', ', combined, states);
if last.orgID then
output;
run;
**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
by orgID;
var states;
run;
data want_option2;
set wide;
length combined $100.;
combined=catx(', ', of state_:);
run;
下面是解决此类问题的两种方法的示例:
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
3 football CA
;
run;
*Sort - required for both options;
proc sort data=have;
by orgID;
run;
**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
set have;
by orgID;
length combined $100.;
retain combined;
if first.orgID then
combined=states;
else
combined=catx(', ', combined, states);
if last.orgID then
output;
run;
**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
by orgID;
var states;
run;
data want_option2;
set wide;
length combined $100.;
combined=catx(', ', of state_:);
run;
你可以先用。通过变量来实现这一点。您必须在datastepSAS中执行此操作,因为它不支持列表或透视/窗口功能。但是,在SAS中有许多其他方法可以完成这些类型的任务。您可以先使用。通过变量来实现这一点。您必须在datastepSAS中执行此操作,因为它不支持列表或透视/窗口功能。但是,在SAS中有许多其他方法来完成这些类型的任务。当
DO
循环中有SET
语句时,不需要IF/THEN/ELSE
。当DO
循环中有SET
语句时,不需要IF/THEN/ELSE
。