我對 sas 相當陌生,我正在解決這個問題。我有兩個表 A 和 B。 第一個收集有關客戶的購買和他們購買的特定產品的資訊
一種
| 顧客ID | 產品代碼 |
|---|---|
| 1111111 | 12345 |
| 1111111 | 34523 |
第二個是某種字典,即包含舊代碼和新代碼,舊代碼是鍵,新代碼是更新版本。
乙
| 老的 | 新的 |
|---|---|
| 34523 | 22256 |
| 89765 | 76576 |
我的目標是在表 A 中更新所有對舊代碼及其更新(新)版本的參考。最后 A 應該看起來像
| 顧客ID | 產品代碼 |
|---|---|
| 1111111 | 12345 |
| 1111111 | 22256 |
我在這個例子中采用的方法如下(偽代碼)
if A.product_code in B.old then
A.product_code = B.new
else
nothing
但是我正在用 sas synthax 來實作它。
我真的希望我的問題足夠清楚,如有必要,請不要猶豫,要求進一步澄清。
感謝任何愿意參與的人
uj5u.com熱心網友回復:
怎么樣
data a;
input Customer_id product_code;
datalines;
1111111 12345
1111111 34523
;
data b;
input old new;
datalines;
34523 22256
89765 76576
;
proc sql;
update a
set product_code =
(select new from b
where a.product_code = b.old)
where exists (
select 1
from b
where a.product_code = b.old)
;
quit;
uj5u.com熱心網友回復:
一種非常 SAS 的方法是使用MODIFY陳述句和哈希查找。
data master;
modify master;
if _n_ = 1 then do;
declare hash mappings(dataset:'code_changes(rename=new_code=code)');
mappings.defineKey('old_code');
mappings.defineData('code');
mappings.defineDone();
call missing(old_code);
end;
if mappings.find(key:code)=0 then replace;
run;
另一種MODIFY方法是使用SET陳述句讀取更改。
此示例需要主表上的索引。
proc sql;
create index code on master;
data master;
set mappings;
reset = 1;
do until (_iorc_);
code = old_code;
modify master key=code keyreset=reset;
if _iorc_ = 0 then do;
code = new_code;
replace;
end;
reset = 0;
end;
_error_ = 0;
run;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354739.html
