通过多个列的不同列名合并SAS数据集

时间:2017-01-12 22:25:59

标签: merge sas

我有2个数据集,我想通过领域合并#...第一个数据集包含领土信息,包括领土#,第二个数据集有领土#,但它们分别是4个不同的列,标题为drug_terr1,drug_terr2,drug_terr3和drug_Terr4 ...我需要在所有4列上合并,因为它们各自具有不同的区域#并且我希望这些数字包含在我与包含所有区域信息的数据集的合并中...我尝试重命名但这没有用,因为它只改变了第一列...是否有办法将所有这些数据合并,并按领域重命名#所以我可以进行合并?

最终希望它看起来像这样,但是需要从'terrfile'中获取4列,成为名为territory_nbr的1列,这样我才能合并。

 %let output = E:\Horizon\Adhoc\AH\;
%let terrs =\\uslsasas1\E$\Horizon\IMS Processing\Weekly Data\20161230\Demo\;
libname terrs "&terrs.";
%let curr_process_wk = '12-30-2016';
%let curr_quarter =_q1;
**0 Grab pskw;
data pskw_data;
set PSKW.PSKWMaster ;
where week in ('12-16-2016','12-23-2016','12-30-2016','01-06-2017') and CopayType ="FBD" and FNRX=1 and pme_id in (46,42,55,38) and product in ('DUEXIS','VIMOVO','PENNSAID')
and
(COBPrimaryRejectCode1 in ('75','76') or COBPrimaryRejectCode2 in ('75', '76') or COBPrimaryRejectCode3  in  ('75' , '76'));
run;
proc sort data=pskw_data;
by imsid;
run;

** 01 Grab tbl HCP;
proc sort data=ims.tblhcp (where = (week = &curr_process_wk.) keep = week imsid first_name last_name address1 address2 city state zip spec)
          out = IMS_demo (drop = week);
      by IMSID;
run;

** 02 Grab tbl terrs_by_imsid;
data terrfile;
set terrs.wd2_terrs_by_imsid&curr_quarter.;
run;

proc sort data = terrfile;
by imsid;
run;
** 03 Grab tbl roster;
data roster (keep = territorycode repname territoryname teamname);
set ims.tblRoster;
   repname = trim(left(FirstName))||" "||trim(left(LastName));
run;
**04 link ;
data combine_dbs;
merge pskw_data (in=in1)
ims.tblhcp (in=in2);
by imsid;
if in1;
run;
data territories;  ***can't merge because territory code is not in terrfile, just 4 columns as I mentioned above***;
merge terrfile (in=in1)
roster (in=in2);
by territorycode;
if in2;
run;

1 个答案:

答案 0 :(得分:1)

您需要将事实表与查找表合并四次。假设您的区域标识符在查找表中被称为ID,您希望从中获取字段IMS_ID。我们还假设您的事实表中的四个字段名为ID1-ID4

proc sql ;
  create table want as 
     select a.*
          , b.ims_id as ims_id1
          , c.ims_id as ims_id2
          , d.ims_id as ims_id3
          , e.ims_id as ims_id4
     from FACT a
     left join LU b on a.id1=b.id
     left join LU c on a.id2=c.id
     left join LU d on a.id3=d.id
     left join LU e on a.id4=e.id
   ;
quit;

在您的示例中,ROSTER表示FACT表,TERRFILES表是LU表。您的ID变量看起来像名称TERRITORYCODE,至少在您的查找文件中。很难说出ROSTER中的四个变量是什么名称。