BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

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.

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Reeza
Super User

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;

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 850 views
  • 0 likes
  • 5 in conversation