I have a dataset with 500 variables, How can I label them without using conventional method of labeling each variable, which is time consuming.
500 variables seems a lot, do you think thats the best data model for what you have, I mean you have already hit labelling them, how are you going to work with such a vast number of variables? As for how to do this, without any information of how your labels are stored, what the data looks like its impossible to say, if the labels are not in the computer, then typing them in is the only method, if they are stored somewhere, then you could generate the code:
data _null_; set label_data end=last; if _n_=1 then call execute('proc datasets lib=test;'); call execute('label '||strip(name)||'="'||strip(label)||'";'); if last then call execute('run;'); run;
Of course this assumes data is in lib temp, you have a dataset with name and label.
Thanks,
Yes, large no of variables and I do have Label stored in another Excel,Will work on this code.
@Deepak03 wrote:
Thanks,
Yes, large no of variables and I do have Label stored in another Excel,Will work on this code.
I have used Excel formulas to combine the variable name and text for a label to create SAS Label syntax. And then copy/ paste into the SAS editor. the formula would look like this in EXCEL
=D134&" = """&H134&""""
The multiple quotes are so the resolved value puts the quotes around the label protion. In the above column D has variable names and
column H had the text of the label.
I could then copy all of the rows with label and paste into SAS.
Use the data to create the LABEL statement(s).
filename code temp;
data _null_;
set metadata ;
put 'label ' name '=' label :$quote. ';' ;
run;
Then use them in your code. Either in the step that makes the data.
data mydata ;
length ..... ;
input .... ;
%include code / source2 ;
run;
Or use PROC DATASETS to apply them to an existing dataset.
proc datasets lib=work nolist ;
modify mydata ;
%include code / source2;
run;
quit;
I have Used 2 datasets which are in Assign library.
PSTN_FIle
Labels
I have Imported them in Assign library (Using SAS University edition)
Used following code :
data _null_;
set ASSIGN.LABELS end=last;
if _n_=1 then call execute('proc datasets library=ASSIGN nolist nodetails ; modify ASSIGN.PSTN_FILE; label');
call execute(cats(Variable,'=',label));
if last then call execute(';quit;');
run;
It's still giving me erros !! any help ?
You need to provide sample data for us. I suggest you post two data steps creating such data.
Data step one creates some sample data with - let's say - three variables, data step two creates the data with the labels for this three variables.
Make sure you use names which are close to what you have in your real data as this will it make easier for you to then transition our propositions to your real data and code.
Sample Fies attached , File to be modified is Sample 2
Do not attach data as files. Paste the code to create the data into the Insert SAS code box. Something like this:
data sample1;
length Variable $32 Label $200 ;
infile datalines dsd dlm='|' truncover ;
input variable label;
datalines;
PRODUCT_ID|Line number
Year|Year
AON|Age on network
;
@Deepak03 wrote:
I have Used 2 datasets which are in Assign library.
PSTN_FIle
Labels
I have Imported them in Assign library (Using SAS University edition)
Used following code :
data _null_;
set ASSIGN.LABELS end=last;
if _n_=1 then call execute('proc datasets library=ASSIGN nolist nodetails ; modify ASSIGN.PSTN_FILE; label');
call execute(cats(Variable,'=',label));
if last then call execute(';quit;');
run;
It's still giving me erros !! any help ?
The MODIFY statement in PROC DATASETS just wants the membername, not the full two level name.
You need quotes around the label value since it wants a string literal.
data _null_;
set ASSIGN.LABELS end=last;
if _n_=1 then do;
call execute('proc datasets library=ASSIGN nolist nodetails ;');
call execute('modify PSTN_FILE; label');
end;
call execute(cats(Variable,'=',quote(trim(label))));
if last then call execute(';quit;');
run;
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 25. 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.