DATA Step, Macro, Functions and more

how do I use meta data extracted from proc contents to read in obs?

Reply
Frequent Contributor
Posts: 133

how do I use meta data extracted from proc contents to read in obs?

I have meta data extracted into data set b like this

proc contents data = t  out = b  noprint nodetails;

run;


in data set b, I just use Name, Type, Length.



Now, I want to create data set "addi" using the data set b, then append the data set addi to data set t.

How do I do that?

I want to do this so that appending data set won't have inconsistent length or data type issue.

Trusted Advisor
Posts: 1,932

Re: how do I use meta data extracted from proc contents to read in obs?

I think you would have to write a macro so that all variables in addi have the same type and length as in data set t.

But I'm curious, why don't you just use PROC APPEND with the FORCE option? Does that not work?

Super User
Posts: 19,861

Re: how do I use meta data extracted from proc contents to read in obs?

Data set t has only name, type and length as well?

It seems weird to want to want to append the contents result to the data you originally ran it on...

Anyways, assuming it makes sense.

data addi;

set b;

keep name type length;

*other things go here;

run;

proc append base=t data=addi force;

run;

Super User
Posts: 5,516

Re: how do I use meta data extracted from proc contents to read in obs?

The usual way to copy all the attributes does not bother with a PROC CONTENTS.  Instead:

data addi;

  if 0 then set t;

  * additional statements to read in data to addi;

run;

Just having SET T at the start of the program will set up all the variable attributes in exactly the same way they are defined in T.  There is no need to actually read in any of the data, hence IF 0 which is always false.

Good luck.

Super User
Super User
Posts: 7,076

Re: how do I use meta data extracted from proc contents to read in obs?

I recommend just using PROC APPEND, but assuming you only have dataset B you can use that to generate a LENGTH statement.

proc sql noprint ;

  select catx(' ',name,cats(case when (type=2) then '$' else ' ' end,length))

    into :varlist separated by ' '

    from b

  ;

quit;

data addi;

  length &varlist ;

  .... code to populate variables  .... ;

run;

It would be better to include VARNUM in the metadata so that you could create the variables in the proper order. (Note generating macro variable DUMMY to suppress SQL message when order variable not included in the SELECT list.)

proc sql noprint ;

  select catx(' ',name,cats(case when (type=2) then '$' else ' ' end,length))

       , varnum

    into :varlist separated by ' '

       , dummy

    from b

    order by varnum

  ;

quit;

Ask a Question
Discussion stats
  • 4 replies
  • 228 views
  • 0 likes
  • 5 in conversation