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?
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;
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?
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?
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.
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.
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: