Alternatively by proc contents as well you will know the column position and variable name as below
proc contents data=sashelp.class;
run;
Lets try to identify the columns name based on the column position, in your example we need to identify the first column so we need to use dictionary.columns as below so based on where varnum variable you will know the column orders and the first column will have varnum=1 and from the name column you will know the variable name to use further. Try this on you dataset
proc sql;
create table want as select * from dictionary.columns where libname='SASHELP' and memname='CLASS';
run;
Alternatively by proc contents as well you will know the column position and variable name as below
proc contents data=sashelp.class;
run;
Why would you say " Proc content can’t help" .
Once running the following code you could get the first variable name .
data have; set sashelp.class; label name='xxxxxx' age='yyyyyy'; run; proc contents data=have varnum; run;
Variables in Creation Order # Variable Type Len Label 1 Name Char 8 xxxxxx 2 Sex Char 1 3 Age Num 8 yyyyyy 4 Height Num 8 5 Weight Num 8
Or Try following code .
data _null_; set have; array n{*} _numeric_; array c{*} _character_; do i=1 to dim(n); if vlabel(n{i})='xxxxxx' then call symputx('first_name',vname(n{i})); end; do i=1 to dim(c); if vlabel(c{i})='xxxxxx' then call symputx('first_name',vname(c{i})); end; run; data want; set have; drop &first_name; run;
The VNEXT call routine will iterate through the PDV variable names. The first call will return the first variable name.
Use the returned variable name in a DOSUBL function call that runs a 'slicing' step.
Example:
data _null_; if 0 then set sashelp.cars; length _name_ $32; call vnext(_name_); rc = dosubl("data want; set sashelp.cars (keep=" || trim(_name_) || ");"); stop; 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.