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?
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.
@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.
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
Other notes:
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.