BookmarkSubscribeRSS Feed
Ron_MacroMaven
Lapis Lazuli | Level 10

This is a non-trivial problem.

 

note.1: where I changed the length of a known variable with the attribute statement

and the var-name does not match the case of the master data set

then that var-name is changed.

Since is this is a demo of proof-of-concept

this may not matter.

 

note.2 be aware of the fragile nature of the cat* functions;

if they return a value larger than $200 then it will truncate your list

solution: add a length=nnnn after the catx function call

 

note.3 data structure may be only <1% of your code

but it is the first place I turn to when my algorithm fails.

Likewise here: I recommend adding the data structure of A

to the code that creates B; this is my recommended solution and solves all later appending.

 

DATA B;
if 0 then set A;
*...;

note.4 this solution assumes both data sets are different

and thus have to be copied in order to change the lengths.

 

hth

Ron Fehd  list processing maven

 

*note case of name matters in output;
data class1;
     attrib sex length = $2;
set sashelp.class;

data class2;
     attrib name length = $10;
set sashelp.class;

%macro save_attrib(libname=work, memname=,out=);
PROC sql noprint;
         create table &out as
         select name, lowcase(name) as name_lc, type, length
         from dictionary.columns
         where libname eq "%upcase(&libname)"
           and memname eq "%upcase(&memname)"
           and memtype eq "DATA";
         quit;
run;
%mend;
%save_attrib(memname=class1,out=contents1)
%save_attrib(memname=class2,out=contents2)

DATA attributes;
set contents1
    contents2;
run;
PROC sort data = &syslast;
          by name_lc length;
run;
DATA attributes;
     retain max_length 0;
set  &syslast;
by   name_lc;
if first.name_lc then max_length = 0;
max_length = max(max_length,length);
if last.name_lc then output;
run;
PROC sql noprint;
         select catx(' ',name,'length='
         ,case when type eq 'char' then '$'
               else                     ' '
               end
         ,length) into :list separated by ' '
         from &syslast;
         quit;
%put echo &=list;
%macro update_attrib(data=,base=largest);
DATA &DATA;
     attrib &list;
set &data;
PROC append data = &data
            base = &base;
run;
%mend;
%update_attrib(data=class1)
%update_attrib(data=class2)

proc print data = &syslast;

PROC sql; describe table &syslast;
          quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 2033 views
  • 0 likes
  • 7 in conversation