3

我在 SAS 中有两个要合并的数据集,但它们没有公共变量。一个数据集有一个“subject_id”变量,而另一个有一个“mom_subject_id”变量。这两个变量都是 9 位代码,代码中间只有 3 位具有共同含义,这就是我在合并它们时需要匹配两个数据集的内容。

我想做的是在每个数据集中创建一个新的公共变量,它只是主题 ID 中的 3 位数字。这 3 位数字将始终位于 9 位主题 ID 中的同一位置,所以我想知道是否有办法从变量中提取这 3 位数字以创建一个新变量。

谢谢!

4

3 回答 3

7

SQL(使用数据步骤代码中的示例数据):

proc sql;
 create table want2 as  
 select a.subject_id, a.other, b.mom_subject_id, b.misc
 from have1 a JOIN have2 b 
  on(substr(a.subject_id,4,3)=substr(b.mom_subject_id,4,3));
quit;

数据步骤:

 data have1;
  length subject_id $9;
  input subject_id $ other $;
  datalines;
   abc001def other1
   abc002def other2
   abc003def other3
   abc004def other4
   abc005def other5
  ;

 data have2;
  length mom_subject_id $9;
  input mom_subject_id $ misc $;
  datalines;
   ghi001jkl misc1
   ghi003jkl misc3
   ghi005jkl misc5
  ;

 data have1;
  length id $3;
  set have1;
  id=substr(subject_id,4,3);
 run;

 data have2;
  length id $3;
  set have2;
  id=substr(mom_subject_id,4,3);
 run;

 Proc sort data=have1;
  by id;
 run;

 Proc sort data=have2;
  by id;
 run;

 data work.want;
  merge have1(in=a) have2(in=b);
  by id;
 run;
于 2012-06-01T22:19:16.187 回答
0

另一种方法是使用

    proc sql

然后使用连接substr() 就像上面解释的那样,如果你对 sql 感到满意的话

于 2012-06-01T22:31:55.403 回答
0

假设您的“subject_id”变量是一个数字,那么该substr函数将无法工作,因为 sas 会尝试将数字转换为字符串。但默认情况下,它会在数字左侧填充一些步数。

您可以使用模数函数,该函数mod(input, base)在输入除以基数时返回余数。

/*First get rid of the last 3 digits*/
temp_var = floor( subject_id / 1000);
/* then get the next three digits that we want*/
id = mod(temp_var ,1000);

或者在一行中:

id = mod(floor(subject_id / 1000), 1000);

然后您可以继续按 id 对新数据集进行排序,然后合并。

于 2014-10-13T12:59:43.867 回答