Labels from one dataset to the next

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Labels from one dataset to the next

Can anyone tell me why I wouldn't keep labels from one dataset to the next?  For example, if running:

 

data work.example1;

   set work.example2;

   (lots of other code in here);

run;

 

Why wouldn't example1 have labels assigned the same as example2?  I'm running a data step like above where new variables are being created and given labels.  The only variables showing up with labels are the new ones being created.

 

Is there an option somewhere that says to "keep" labels based on tables being used to create new ones?


Accepted Solutions
Solution
‎06-02-2016 11:13 AM
Grand Advisor
Posts: 10,193

Re: Labels from one dataset to the next

Proc datasets with the modify statement will allow you to set or change labels for an existing data set. If you have the variable names and the label you want in a dataset then you could use call execute.

Or use the set to write a label block of code that you could %include where needed. The only lines needed with the approach would be to save a file named something like: "C:\path\mylabels.sas";

with contents like:

label

   var1 = "Label for the first variable"

   var2 = "Label for the second variable"

;

 

and use:

 

data whatever;

    set anotherdata set;

   %include "C:\path\mylabels.sas";

    <other code>

run;

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,189

Re: Labels from one dataset to the next

Are you sure the contributiing dataset has labels?  If so post an example, simple dataset with labels, and your code.  The reason is this:

data tmp;
  set sashelp.cars;
  label abc="A new variable";
  abc="Abcd";
run;

Shows the labels from cars and the one I created no problem.   You sure your not modifying the variables which are coming in anyway?

Occasional Contributor
Posts: 9

Re: Labels from one dataset to the next

 

I do know the labels are assigned in the contributing table.  I am running a proc contents on the contributing table and see the labels printed in the output.

 

The values of the variables coming in are being changed but nothing else about the variable is changed (ie. length, type). Will I lose the label if ANY change is made to the variable?

Occasional Contributor
Posts: 9

Re: Labels from one dataset to the next

lf the label is dropped from the contributing table because the value is being changed when creating a new dataset is there a way I can re-assign the labels without having to enter them again?  For example could I create code that utilizes the proc contents output from the contributing table and if the new table doesn't have a label it will check the contributing table for a label and use it if it's there?

 

I think I know the answer to this question, just need to figure out the appropriate macros to use to obtain the data.

 

Thank you.

Occasional Contributor
Posts: 9

Re: Labels from one dataset to the next

Can I delete this question?

 

I see some macros being used that are renaming the variables.

 

I need to create code that will get the labels from previous and assign them to the new.  (This code is crazy with renaming variables throughout the entire process. This is something I plan to change once I can be sure of which variable is which throughout.

Solution
‎06-02-2016 11:13 AM
Grand Advisor
Posts: 10,193

Re: Labels from one dataset to the next

Proc datasets with the modify statement will allow you to set or change labels for an existing data set. If you have the variable names and the label you want in a dataset then you could use call execute.

Or use the set to write a label block of code that you could %include where needed. The only lines needed with the approach would be to save a file named something like: "C:\path\mylabels.sas";

with contents like:

label

   var1 = "Label for the first variable"

   var2 = "Label for the second variable"

;

 

and use:

 

data whatever;

    set anotherdata set;

   %include "C:\path\mylabels.sas";

    <other code>

run;

Contributor
Posts: 53

Re: Labels from one dataset to the next

If you assigned labels using the ATTRIB or the LABEL statements, they should stick.

 

But.

 

If you did this -

 

data one;

attrib var1 label = 'Variable 1';

var1 = 1;

 

data two (keep = var2 rename=(var2=var1));

/* attrib var2 label = 'Variable 2'; */

set one;

var2 = var1;

var3 = var1;

run;

 

proc print data=two; run;

 

No, proc print won't show 'Variable 1' for var1 or var3.

If you uncommented the attrib statement, then the proc print should show the label 'Variable 2' for var1.

 

If you could post some part of the problem code here, we should be able to identify the problem.

 

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 413 views
  • 0 likes
  • 4 in conversation