So, I am brand new to SAS. New as in Thursday. I really need to create a key that pairs up the name with the labels in a large data set. All of the names are simple letter/number combinations and the actual labels are what make sense. So, I just need to figure out how to create a two-column table that lists the names beside the labels. I really would like to keep my job.
Are talking about the variable names and the labels that are attached to the variables?
You can use PROC TRANSPOSE on the original data with OBS=0 dataset option and the output dataset will have one observation per variable with the variable _NAME_ having the name of teh variable and the variable _LABEL_ having the label. Note the _LABEL_ variable is only created if at least one of the variables actually has a label attached.
proc transpose data=have(obs=0) out=want;
var _all_;
run;
Or are you talking about two varaibles in your dataset. One that has the code (name) and one that has the decode (label)?
If so then PROC FREQ is probably your answer.
proc freq data=have ;
tables code_var * decode_var / noprint missing out=want;
run;
You will also get a variable COUNT that has how often that combination appears.
Or are you talking about a custom format that have attached to a variable?
In that case use PROC FORMAT to create a dataset from the format definition. You need to know the name of the format in question. You also might need to use the LIB= (aka CAT=) option on the PROC FORMAT statement to tell it which format catalog to search for the format definition.
proc format cntlout=want;
select myformat ;
run;
In which form do you have those name/label pairs? Text file, Excel, or already as SAS dataset?
It is in a SAS dataset.
@livkailyn wrote:
It is in a SAS dataset.
What is in a SAS dataset? Two variables? What are their names; Types; If character the length; any attached formats, ....
If you need to create a table with variable names and -labels from an existing dataset, you pull that from dictionary.columns in SQL or sashelp.vcolumn in a data step.
Here's a good way to explain problems in a way that we all will understand.
For example:
I have a large data set that has names and labels like below names and labels are repeated multiple times.
ID Names Labels 1 A1 Physics 10 1 A2 Math 10 2 B1 Physics 20 2 B2 Math 20
3 A1 Physics 10
3 A2 Math 10
4 B1 Physics 20
6 B2 Math 20
I need to create this data set in SAS such that I have only the names and labels and uniquely list such as:
Name Label
A1 Physics 10
A2 Math 10
....
If you can't post your real data make up fake data in Excel and post that or use a data set from the SASHELP library. CLASS is the most commonly used data set.
Then you'll get an answer like the following:
Use PROC FREQ to summarize the list and OUT to store it into a data set. It will include counts and percents but you can drop those with a DROP statement.
proc freq data=have;
table names*labels / out=want;
run;
proc print data=want;
run;
PROC FREQ MEANS and TRANPOSE should be your best friends.
Take the free SAS e-course (it's all online) first it's worth the two days to learn things and you'll be a lot faster afterwards and less stressed about keeping your job.
@livkailyn wrote:
So, I am brand new to SAS. New as in Thursday. I really need to create a key that pairs up the name with the labels in a large data set. All of the names are simple letter/number combinations and the actual labels are what make sense. So, I just need to figure out how to create a two-column table that lists the names beside the labels. I really would like to keep my job.
Hi @livkailyn
If I understand your question, you want to create a new data set with a list of all variables + labels in an existing SAS data set. Proc contents is one way to do it. The following code creates a variable list from the sashelp data set baseball as example.
It lists the variables in alphabetical order, and the variable position is included, so you can sort the data set afterwards to get the variables in the same order as you see in the input data set.
proc contents data=sashelp.baseball out=varlist (keep=name label varnum);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.