SAS宏变量distinct

huangapple go评论60阅读模式
英文:

SAS macro variable distinct

问题

以下是已翻译的内容:

我想请删除KI表中插入的所有重复项,知道我有从2007年到2022年的表。

英文:

I want to please delete all the duplicates inserted in the KI table, knowing that I have the tables from 2007 to 2022

%do I=2007 %to 2022 ;
proc sql noprint;
select nliteral(name) into :varlist separated by ','
   from dictionary.columns
   where libname='SI' and memname="SI&I."
;
insert into LIB1.KI (&varlist) select &varlist
from SI.SIQ&I.;
quit;
%end;
%mend;
%KI;

答案1

得分: 1

以下是翻译好的部分:

这是一种方法

选项 nodsnferr;
数据 lib1.ki;
  集 lib1.si2007-lib1.si2022;
运行;
选项 dsnferr;

过程排序 数据=lib1.ki 无重复键;
  按 _all_;
运行;

没有假设相同性或强制验证的情况下,这个问题充满了诸如以下问题:

  • 变量类型不一致
  • 变量长度不一致
  • 值模糊不一致
  • 变量名称(作为一组)不一致

最初的天真是

创建表 newnique 作为 
选择 * 从数据
其中 catx('~', &varlist) 不在 (选择 catx('~', &varlist) 从基础)

插入到基础
选择 * 从 newnique;
英文:

Here is one way

options nodsnferr;
data lib1.ki;
  set lib1.si2007-lib1.si2022;
run;
options dsnferr;

proc sort data=lib1.ki nodupkey;
  by _all_;
run;

With out suppositions of sameness or enforced validations the problem is fraught with issues such as:

  • variable type discrepancies
  • variable length discrepancies
  • value fuzz discrepancies
  • variable names (as a set) discrepancies

The initial naive is

create table newnique as 
select * from data
where catx('~', &varlist) not in (select catx('~', &varlist) from base)

insert into base
select * from newnique;

答案2

得分: 0

如果您只想在当前的INSERT语句中插入来自SI.SIQ&I的不同观察结果,请在SELECT表达式中添加DISTINCT关键字。

insert into LIB1.KI (&varlist)
select distinct &varlist
from SI.SIQ&I;

如果您需要不插入已经存在的观察结果,那么您需要更努力地删除LIB1.KI中存在的观察结果,以避免与SI.SIQ&I中的观察结果重复。我怀疑您需要创建一个临时数据集来执行此操作,因为您需要从同一LIB1.KI数据集中读取并进行排除测试,而同时又将数据插入其中。

您可以像这样创建这样的数据集:

create table to_add as 
select &varlist from SI.SIQ&i
except
select &varlist from LIB1.KI;

然后在INSERT语句中使用它,而不是SI.SIQ&i。

英文:

If you only want to insert distinct observations from SI.SIQ&I in the current INSERT statement add the DISTINCT keyword to the SELECT expression.

insert into LIB1.KI (&varlist)
 select distinct &varlist
 from SI.SIQ&I.
;

If you need to not insert observations that already exist then you will need to work harder to remove the observations that exist in LIB1.KI from the observations in SI.SIQ&I. I suspect you will have to make a temporary dataset to do that since you would need to test for exclusion by reading from the same LIB1.KI dataset that you are inserting into.

You might do something like this to create such a dataset .

 create table to_add as 
    select &varlist from SI.SIQ&i. 
    except
    select &varlist from LIB1.KI
 ;

Then use that in the INSERT statement instead of SI.SIQ&i.

huangapple
  • 本文由 发表于 2023年6月12日 16:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定