有一个以CSV格式提供给我的样本数据集。 虚拟数据集如下:
Baseball1,Baseball2
USA,France
USA,Italy
USA,England
England,USA
England,Australia
England,Sri Lanka
France,USA
France,England
France,Italy
Italy,USA
Italy,France
Italy,England
我需要获取输出数据,其中数据仅具有离散值。 期望的输出如下:
Baseball1 Baseball2
USA France
USA Italy
USA England
England Australia
England Sri Lanka
France England
France Italy
Italy England
我认为PROC SQL可能在这里工作,但我不知道如何删除不同列的重复条目。
答案 0 :(得分:2)
我认为棘手的部分是你关心变量的水平顺序,所以对你来说法国/意大利和意大利/法国实际上形成了你想要删除的副本。
请参阅下面的代码,注意它的作用:
/* Reading data in */
data have;
length baseball1 $ 9 baseball2 $ 9;
infile datalines delimiter=',';
input Baseball1 $ Baseball2 $ ;
datalines;
USA,France
USA,Italy
USA,England
England,USA
England,Australia
England,Sri Lanka
France,USA
France,England
France,Italy
Italy,USA
Italy,France
Italy,England
;
/* horizontal sorting */
data sorted_arrays;
set have;
length Team1 $ 9 Team2 $ 9;
/* Copying data into new vars to preserve original data for output */
Team1 = Baseball1;
Team2 = Baseball2;
/* Sorting data horizontally with sortc call */
call sortc(Team1,Team2);
/* Creating an ID by concatenating sorted variables */
ID = (CATX("/",Team1,Team2));
/* Preserving original order */
order = _N_;
run;
/* Removing duplicates by ID and keeping required variables*/
PROC SORT data=sorted_arrays out=no_dupes(keep=baseball1 baseball2 order) NODUPKEY;
BY ID;
RUN;
/* Returning to original order to achieve the result needed */
PROC SORT data=no_dupes out=want(drop=order);
by order;
run;
/* Final Report*/
PROC PRINT data=want;
RUN;
结果:
如果变量的最终水平/垂直顺序无关紧要,您可以按如下方式简化代码,并且可以使用PROC SQL:
/* Reading data in */
data have;
length baseball1 $ 9 baseball2 $ 9;
infile datalines delimiter=',';
input Baseball1 $ Baseball2 $ ;
/* horizontal sorting */
call sortc(Baseball1,Baseball2);
datalines;
USA,France
USA,Italy
USA,England
England,USA
England,Australia
England,Sri Lanka
France,USA
France,England
France,Italy
Italy,USA
Italy,France
Italy,England
;
/*Remove dupes */
PROC SQL;
CREATE TABLE want AS
SELECT DISTINCT t1.baseball1,
t1.baseball2
FROM WORK.HAVE t1;
QUIT;
/* Final Report*/
PROC PRINT data=want;
RUN;
结果:
/* Reading data in */
data have (drop=tmp);
length baseball1 $ 9 baseball2 $ 9 tmp $9;
infile datalines delimiter=',';
input Baseball1 $ Baseball2 $;
/* horizontal sorting */
if Baseball1>Baseball2 then
do;
tmp = Baseball1;
Baseball1=Baseball2;
Baseball2 = tmp;
end;
datalines;
USA,France
USA,Italy
USA,England
England,USA
England,Australia
England,Sri Lanka
France,USA
France,England
France,Italy
Italy,USA
Italy,France
Italy,England
;
/*Remove dupes */
PROC SQL;
CREATE TABLE want AS
SELECT DISTINCT t1.baseball1,
t1.baseball2
FROM WORK.HAVE t1;
QUIT;
/* Final Report*/
PROC PRINT data=want;
RUN;
与前一个示例中的结果相同: