简体   繁体   English

SAS:如何垂直连接多个数据集,其中变量在一个数据集中为数字,在另一个数据集中为字符

[英]SAS: How to vertically join multiple datasets where a variable is numeric in one dataset and character in the other

This is complicated a little because we're using a pipeline with a filelist to compile the data, so there are 50+ datasets coming in. I need to combine many, many datasets vertically, but var2 is numeric in some and character in others.这有点复杂,因为我们使用带有文件列表的管道来编译数据,所以有 50 多个数据集进来。我需要垂直组合很多很多数据集,但是 var2 在一些中是数字,在其他中是字符。 Var1 is not important, so we can drop it, but when I try to drop it in the data step, it is throwing an error because of the differing data types. Var1 不重要,所以我们可以删除它,但是当我尝试在数据步骤中删除它时,由于数据类型不同,它会抛出错误。 More details below.更多细节如下。

Here's what I want to do at it's most basic...这是我想做的最基本的事情......

data in1;数据输入 1; input var1 $ var2 datalines;输入 var1 $ var2 数据线; a 1 b 2;一个 1 b 2;

data in2;数据输入2; input var1 $ var2 $ datalines;输入 var1 $ var2 $ 数据线; a 1a b 2b;一个 1a b 2b;

data newdd;数据newdd; set in1 in2;设置 in1 in2; run;跑步;

Is it possible to combine these datasets in the "data newdd" step without changing the inputs?是否可以在不更改输入的情况下在“data newdd”步骤中组合这些数据集? Is there a way to drop var2 in this data step in a way that will still let it merge var1 and not throw an error?有没有办法在这个数据步骤中以一种仍然让它合并 var1 而不会抛出错误的方式删除 var2? Or better yet, can I make var2 read in as character in all cases?或者更好的是,我可以在所有情况下都将 var2 作为字符读入吗?

To drop var2 use a data set option.要删除 var2,请使用数据集选项。

data newdd;
set in1(drop=var2) in2(drop=var2);
run;

To combine and ensure it's the same:合并并确保它是相同的:

data newdd;
set in1 (in=t1 rename=var2=_var2) in2(in=t2 );
if t1 then var2 = put(_var2, 8. -l);
run; 

Long answer - you need to fix how you read in the files - all 50 from source so they're consistent.长答案——你需要修正你在文件中的阅读方式——全部 50 个来自源代码,以便它们是一致的。 You can have SAS generate the correct types/INPUT statement if you have a master list of variables and type/length/format/informat, type at minimum.如果您有变量的主列表和类型/长度/格式/信息格式,则可以让 SAS 生成正确的类型/INPUT 语句,至少键入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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