SAS循环遍历数据集表进行比较。

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

SAS Loop through a table of datasets to compare

问题

我有两个文件夹(librefs),其中包含许多数据集。每个文件夹都包含相同的数据集,我想比较每个文件夹中的特定数据集(而不是全部数据集),以确保它们是相似的。是否可以循环遍历每个文件夹并比较每个数据集?

proc sql;
   create table specific_tables (table_name CHAR(100));
     insert into specific_tables
     values('name_of_dataset1')
     values('name_of_dataset2')
     values('name_of_dataset3')
     values('name_of_dataset4')
     values('name_of_dataset5')
;
quit;

%macro compare;

libname a "文件夹路径";
libname b "文件夹路径";

%do i=1 %to number of datasets to compare;

proc compare base = a.name_of_dataset&i
compare= b.name_of_data&I
run;

 %end;
%mend;

这样的代码可以帮助你循环遍历每个数据集并进行比较。你需要将"文件夹路径"替换为实际的文件夹路径,并将"number of datasets to compare"替换为要比较的数据集数量。

英文:

I have two folders (librefs) containing numerous datasets. Each folder contains identical datasets and I'd like to compare SPECIFIC datasets (not all) within each folder to ensure they are similar. Is it possible to loop through each folder and compare each dataset?

proc sql;
   create table specific_tables (table_name CHAR(100));
     insert into all specific_tables
     values(name_of_dataset1)
     values(name_of_dataset2)
     values(name_of_dataset3)
     values(name_of_dataset4)
     values(name_of_dataset5)
;
quit;

Both folders contain these datasets. How can I loop through this table of datasets and compare them from each folder?

%macro compare;

libname a "file path";
libname b "file path";

%do i %to number of datasets to compare;

proc compare base = a.name_of_dataset&i
compare= b.name_of_data&I
run;

 %end;
%mend;

答案1

得分: 2

我建议稍微调整一下结构。

  • 分配库
  • 创建一个宏来进行比较,参数是数据集名称
  • 在表列表中的每个表上调用该宏
* 分配库;
libname a "文件路径";
libname b "文件路径";

* 定义比较的宏;
%macro compare(dsn=);

* 运行比较;
proc compare base = a.&dsn
compare= b.&dsn;
run;

* 将结果存储在宏变量中;
%let rc = &sysinfo.;

* 将结果保存到数据集中;
data _temp;
length dsn $32. results 8.;
dsn = "&dsn.";
results = ifn(&rc=0, 0, 1);
run;

* 追加到数据集以保留多次迭代的结果;
proc append base=comparison_results data=_temp;
run;

* 在运行之间进行清理以防止问题;
proc sql;
drop table _temp;
quit;

%symdel rc;

%mend;


* 对列表中的每个数据集调用宏;
data execute;
set specific_tables;

str = catt('%compare(dsn=', table_name, ');');

call execute(str);

run;
英文:

I would recommend a slightly different structure.

  • Assign library
  • Create macro to do comparison, with the parameter being the data set name
  • Call the macro once for each table in the list of tables
*assign libraries;
libname a "file path";
libname b "file path";

*define macros for comparison;
%macro compare(dsn=);

*run comparison;
proc compare base = a.&dsn
compare= b.&dsn;
run;

*store results in macro variable;
%let rc = &sysinfo.;

*save results into dataset;
data _temp;
length dsn $32. results 8.;
dsn = "&dsn.";
results = ifn(&rc=0, 0, 1);
run;

*append to data set to keep over iterations;
proc append base=comparison_results data=_temp;
run;

*clean up between runs to prevent issues;
proc sql;
drop table _temp;
quit;

%symdel rc;

%mend;


*call macro once for each dataset in list;
data execute;
set specific_tables;

str = catt('%compare(dsn=', table_name, ');');

call execute(str);

run;

huangapple
  • 本文由 发表于 2023年8月9日 04:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862936.html
匿名

发表评论

匿名网友

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

确定