SAS PROC SQL - Concatenate variable values into a single value by group

南楼画角 提交于 2019-12-11 14:49:01

问题


I have a data set which contains 'factor' values and corresponding 'response' values:

data inTable;
  input fact $ val $;
  datalines;
  a 1
  a 2
  a 3
  b 4
  b 5
  b 6
  c 7
  d 8
  e 9
  e 10
  f 11
;
run;

I want to aggregate response options by factor, i.e. I need to get

I know perfectly well how to implement this in a data step running a loop through values and applying CATX (posted here). But can I do the same with PROC SQL, using a combination of GROUP BY and some character analog of SUM() or CATX()?

Thanks for help,

Dmitry


回答1:


The data step is the appropriate tool to use in SAS if you want to apply any sort of logic that carries lots of values forward from previous rows.

Any SQL solution would be extremely unwieldy - you would need to join the input table to itself n times, where n is the maximum number of distinct values for any of your factors, and you would also need to define a sequential key preserving the row order to use for the join.

A list of aggregation functions you can use in proc sql is available here: http://support.sas.com/kb/25/279.html

Although a few of these do work with character variables, there is no aggregation function for string concatenation.



来源:https://stackoverflow.com/questions/47434677/sas-proc-sql-concatenate-variable-values-into-a-single-value-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!