开发者

Is there a way to append two datasets so that the longer format takes precedence?

开发者 https://www.devze.com 2023-02-02 21:37 出处:网络
Consider this simple example: data abc; length a $2 b $1; a = \"aa\"; b= \"b\"; run; data def; length a $1 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";
r开发者_StackOverflowun;

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.


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
   */
0

精彩评论

暂无评论...
验证码 换一张
取 消