BookmarkSubscribeRSS Feed
Deepak03
Calcite | Level 5

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

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Deepak03
Calcite | Level 5

Thanks,

 

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

ballardw
Super User

@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.

Tom
Super User Tom
Super User

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;
Deepak03
Calcite | Level 5

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 ?

Patrick
Opal | Level 21

@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.

Deepak03
Calcite | Level 5

Sample Fies attached , File to be modified is Sample 2

Tom
Super User Tom
Super User

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
;

 

Patrick
Opal | Level 21

@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;

 

Tom
Super User Tom
Super User

@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;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2905 views
  • 1 like
  • 5 in conversation