DATA Step, Macro, Functions and more

How do I create LABEL for large no of Variables ?

Reply
New Contributor
Posts: 4

How do I create LABEL for large no of Variables ?

I have a dataset with 500 variables, How can I label them without using conventional method of labeling each variable, which is time consuming.

Super User
Super User
Posts: 7,961

Re: How do I create LABEL for large no of Variables ?

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.

New Contributor
Posts: 4

Re: How do I create LABEL for large no of Variables ?

Thanks,

 

Yes, large no of variables and I do have Label stored in another Excel,Will work on this code.

Super User
Posts: 11,343

Re: How do I create LABEL for large no of Variables ?


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.

Super User
Super User
Posts: 7,050

Re: How do I create LABEL for large no of Variables ?

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;
New Contributor
Posts: 4

Re: How do I create LABEL for large no of Variables ?

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 ?

Respected Advisor
Posts: 4,173

Re: How do I create LABEL for large no of Variables ?

[ Edited ]

@Deepak03

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.

New Contributor
Posts: 4

Re: How do I create LABEL for large no of Variables ?

Sample Fies attached , File to be modified is Sample 2

Super User
Super User
Posts: 7,050

Re: How do I create LABEL for large no of Variables ?

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
;

 

Respected Advisor
Posts: 4,173

Re: How do I create LABEL for large no of Variables ?

[ Edited ]

@Deepak03

Something like below should work (same as @Tom already posted):

filename codegen temp;
data _null_;
  file codegen;
  set labels end=last;
  if _n_=1 then put 'attrib';
  put variable "label ='" label +(-1) "'";
  if last then put ";";
run;

data have;
  set have;
  %include codegen / source2;
run;

 

Super User
Super User
Posts: 7,050

Re: How do I create LABEL for large no of Variables ?

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 10 replies
  • 184 views
  • 1 like
  • 5 in conversation