简体   繁体   中英

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

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, ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM