BookmarkSubscribeRSS Feed
Pre1
Calcite | Level 5

I am a beginner to SAS - I am not super familiar with macros.

 

I need to create labels for 300+ variables in my dataset.

 

Variable names and labels are stored both in a textfile, and an excel file
(Column 1 is the variable name, column 2 is the associated label)

 

How would I go about doing this? Are macros the best solution?

8 REPLIES 8
Kurt_Bremser
Super User

You can create the PROC DATASETS code in a DATA _NULL_ step with CALL EXECUTE, or write it to an external file that you later execute with %INCLUDE.

Reeza
Super User
Here's one way.
https://gist.github.com/statgeek/f18931085f6a0009185c

Because you have 300 variables the macro approach may not work exactly as shown, you may need to loop each rename or use CALL EXECUTE instead. If you search on here, I know I've coded that solution at least twice 🙂
Pre1
Calcite | Level 5
Thank you, so something like this?
https://communities.sas.com/t5/SAS-Programming/How-do-I-create-LABEL-for-large-no-of-Variables/td-p/... Would I need the code you linked, or just this one? For both to work, I have to import the excel with the names into SAS as a separate dataset, right?
Reeza
Super User
You need just one solution, and you do need to import the data from Excel into SAS to use it with either dynamic method except the copy/paste option.
Reeza
Super User
And the really quick & lazy solution, in Excel, create a new column and add in the function that does the following:

=concatenate(variablename, " = ", "'", variableLabel, "'")

That should create the text as follows in your Excel file:

age = 'Age in Years'
name = 'Name'
weight = 'Weight(lbs)'

Label + copy paste that new column from excel + semicolon and you're done.
Pre1
Calcite | Level 5
And just copy and paste that into SAS? That's doable but I guess the code will be very long
ballardw
Super User

@Pre1 wrote:
And just copy and paste that into SAS? That's doable but I guess the code will be very long

Only 300? Not that long.

I had projects using provided documentation that used the "write code in excel" to create labels for way more variables than that. And to create the SAS variable names from some way-too-long text (120 characters in some cases), the input statement and informat as well. Something like 2600 lines of data step code. Almost everything except the Data , Infile and Run.

 

Which is possible with people that actually document stuff and will provide the documentation before guessing what is in the raw files.

mkeintz
PROC Star

Let's say your text file of varnames and labels in the format in my datalines data below.  Then you could read that text file and write the properly composed components of a label statement to a temporary file.  That temporary file can be %INCLUDEd in a PROC DATASETS

 

Something like:

 

filename lbls temp;
data _null_;
  input vname :$32. vlabel &$60.;
  file lbls;
  put @4 vname    @38 "='" vlabel "'";

datalines;
aaaaa        This is a label for aaaaa
bbbbbbbbbbb  This is a label for bbbbbbbbbbb
run;


proc datasets library=mylib nolist;
  modify have;
  label
  %include lbls /source2; 
  ;
quit;

This program assumes

  1. you have a dataset named mylib.have which contains variables aaaaa and bbbbbbbbbbb.
  2. Your text file of varnames and labels looks like my datalines data.

Other notes:

  1. The filename lbls is assigned the TEMP attribute, which means it will be deleted at the end of your sas session.  You don't need to do housekeeping.
  2. The label statement in the proc datasets has three components:
    1. The command "label"
    2. The %INCLUDEd text from the lbls file, which was generated in the prior data step.
    3. Two (!!) semicolons - one to terminate the %include statement, and the other to terminate the multi-component label statement.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 8 replies
  • 1055 views
  • 2 likes
  • 5 in conversation