有没有办法附加两个数据集,以便较长的格式优先?

发布于 2024-10-10 15:49:09 字数 319 浏览 7 评论 0原文

考虑这个简单的示例:

data abc;
  length a $2 b $1;
  a = "aa";
  b= "b";
run;

data def;
  length a $1 b $2;
  a = "a";
  b= "bb";
run;

data ghi;
   set abc def;
run;

在此示例中,数据集 ghi 有两个变量,但它们的长度由数据集 abc 中的内容决定。有没有一种方法(无需编写宏)附加两个数据集,以便如果变量名称相同,则较长的长度优先?也就是说,在此示例中,数据集 ghi 中的 a 和 b 的长度均为 2。

Consider this simple example:

data abc;
  length a $2 b $1;
  a = "aa";
  b= "b";
run;

data def;
  length a $1 b $2;
  a = "a";
  b= "bb";
run;

data ghi;
   set abc def;
run;

In this example the dataset ghi has two variables but their length is determined by what's in dataset abc. Is there a way (without writing macros) to append two datasets so that if the variable names are the same the longer length takes precedence? That is in this example both a and b in dataset ghi is of length 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2024-10-17 15:49:09

如果您没有太多变量,您可以手动为组合数据集生成长度语句,如下所示。请注意,宏变量值的长度有 32k 字符的限制。这也可能会打乱变量的现有顺序。

   /* test data */
   data abc;
     length a $2 b $1;
     a = "aa";
     b= "b";
   run;

   data def;
     length a $1 b $2;
     a = "a";
     b= "bb";
   run;

   /* max lenghs for each and every char type var */
   proc sql;
     create view abcview as
     select name, length
     from dictionary.columns 
     where libname="WORK" and memname="ABC" and type="char";

     create view defview as
     select name, length
     from dictionary.columns
     where libname="WORK" and memname="DEF" and type="char";

     select catx(" $", a.name, max(a.length, d.length)) 
        into :lengths separated by " "
     from abcview as a, defview as d
     where a.name = d.name;
   quit;

   data ghi;
     length &lengths;
     set abc def;
   run;

   proc contents data=ghi;
   run;
   /* on lst - in part
       #    Variable    Type    Len
       1    a           Char      2
       2    b           Char      2
   */

If you don't have very many variables, you can manually generate the length statement for your combined dataset like below. Notice that there is the 32k char limit on the length of the macro variable value. This may also mess up the existing order of the variables.

   /* test data */
   data abc;
     length a $2 b $1;
     a = "aa";
     b= "b";
   run;

   data def;
     length a $1 b $2;
     a = "a";
     b= "bb";
   run;

   /* max lenghs for each and every char type var */
   proc sql;
     create view abcview as
     select name, length
     from dictionary.columns 
     where libname="WORK" and memname="ABC" and type="char";

     create view defview as
     select name, length
     from dictionary.columns
     where libname="WORK" and memname="DEF" and type="char";

     select catx(" $", a.name, max(a.length, d.length)) 
        into :lengths separated by " "
     from abcview as a, defview as d
     where a.name = d.name;
   quit;

   data ghi;
     length &lengths;
     set abc def;
   run;

   proc contents data=ghi;
   run;
   /* on lst - in part
       #    Variable    Type    Len
       1    a           Char      2
       2    b           Char      2
   */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文