0
votes

I am working with multiple waves of survey data. I have finished defining formats and labels for the first wave of data.

The second wave of data will be different, but the codes, labels, formats, and variable names will all be the same. I do not want to define all these attributes again...it seems like there should be a way to export the PROC CONTENTS information for one dataset and import it into another dataset. Is there a way to do this?

The closest thing I've found is PROC CPORT but I am totally confused by it and cannot get it to run.


(Just to be clear I'll ask the question another way as well...)

When you run PROC CONTENTS, SAS tells you what format, labels, etc. it is using for each variable in the dataset.

I have a second dataset with the exact same variable names. I would like to use the variable attributes from the first dataset on the variables in the second dataset. Is there any way to do this?

Thanks!

3
What do you mean by use the variable attributes from the first dataset on the second? Is this on a proc import step, to apply formats/labels to the final data set?Reeza

3 Answers

1
votes

So you have a MODEL dataset and a HAVE dataset, both with data in them. You want to create WANT dataset which has data from HAVE, with attributes of MODEL (formats, labels, and variable lengths). You can do this like:

data WANT ;
  if 0 then set MODEL ;
  set HAVE ;
run ;

This works because when the DATA step compiles, SAS builds the Program Data Vector (PDV) which defines variable attributes. Even though the SET MODEL never executes (because 0 is not true), all of the variables in MODEL are created in the PDV when the step compiles.

Importantly, note that if there are corresponding variables with different lengths, the length from MODEL will determine the length of the variable in WANT. So if HAVE has a variable that is longer than the same-named variable in MODEL, it may be truncated. Options VARLENCHK determines whether or not SAS throws a warning/error if this happens.

That assumes there are no formats/labels on the HAVE dataset. If there is a variable in HAVE that has a format/label, and the corresponding variable in MODEL does not have a format/label, the format/label from HAVE will be applied to WANT.

Sample code below.

data model;
  set sashelp.class;
  length FavoriteColor $3;
  FavoriteColor="Red";
  dob=today();
  label 
    dob='BirthDate'
  ; 
  format 
    dob mmddyy10.
  ;
run;

data have;
  set sashelp.class;
  length FavoriteColor $10;
  dob=today()-1;
  FavoriteColor="Orange";
  label
    Name="HaveLabel"
    dob="HaveLabel"
  ;
  format 
    Name $1.
    dob comma.
  ;
run;

options varlenchk=warn;

data want;
  if 0 then set model;
  set have;
run;
1
votes

I'd create an empty dataset based on the existing one, and then use proc append to append the contents to it.

Create some sample data for the second round of data:

data new_data;
  age = 10;
run;

Create an empty dataset based on the original data:

proc sql noprint;
  create table want like sashelp.class;
quit;

Append the data into the empty dataset, retaining the details from the original:

proc append base=want data=new_data force nowarn;
run;

Note that I've used the force and nowarn options on proc append. This will ensure the data is appended even if differences are found between the two datasets being used. This is expected if you have, for example, format differences. It will also hide things like if columns exist in the new table that aren't in the old table etc. So be careful that this is doing what you want it to. If the behaviour is undesirable, consider using a datastep to append instead (and list the want dataset first).

0
votes

Welcome to the stack.

If you want to copy the properties of the table without the data within it, you could use PROC SQL or data step with zero rows read in.

This examples copies all information about the SASHELP.CLASS dataset into a brand new dataset. All formats, attributes, labels, the whole thing is copies over. If you want to only copy some of the columns, specify them in select clause instead of asterix.

PROC SQL outobs=0;
    CREATE TABLE WANT as SELECT * FROM SASHELP.CLASS;
QUIT;

Regards, Vasilij