BookmarkSubscribeRSS Feed
NKormanik
Barite | Level 11

 

Because of variable length limits, etc., I simply renamed all variables:

 

i_01

i_02

i_03

 

But each variable really should be associated with a longer, more descriptive name:

 

Abc.......

Def.......

Ghi.......

 

I've imported the dataset using the simple variable names.  But I'd like to somehow 'import' the longer respective names as 'labels'.  Can that be done?  If so, how?

 

Thanks,

Nicholas Kormanik

 

3 REPLIES 3
Patrick
Opal | Level 21

I've imported the dataset using the simple variable names.

HOW did you import the data. In what format is the source? Please provide a bit more detail.

 

sbxkoenk
SAS Super FREQ

If you can make a dataset that has the short names in one column and the long names in a second column, then you can proceed as in the program below ... (the long names will serve as label).

data have;
 set sashelp.class;
run;

data short_and_long;
LENGTH varname $ 6 varlabel $ 20; 
varname='Name';   varlabel='xxxxx Name xxxxx';   output;
varname='Sex';    varlabel='xxxxx Sex xxxxx';    output;
varname='Age';    varlabel='xxxxx Age xxxxx';    output;
varname='Height'; varlabel='xxxxx Height xxxxx'; output;
varname='Weight'; varlabel='xxxxx Weight xxxxx'; output;
run;

filename shorlong temp;

data _NULL_;
 set short_and_long end=last;
 file shorlong; 
 if _N_=1 then do;
PUT 'proc datasets library=work NoList NoDetails memtype=data;';
PUT ' modify have;';
 end;
PUT "label " varname "='" varlabel +(-1) "';";
 if last then do;
PUT 'run; QUIT;';
 end;
run;

%INCLUDE shorlong / source2;
/* end of program */

Koen

A_Kh
Barite | Level 11

Similar approach to the above, but using sql and macro:

/*Creating a macro variable "LABELS" that contains variable names and attributed labels*/
proc sql noprint;
	create table have as
		select distinct mnemonic, text 
			from sashelp.webmsg where length(text) eq 55;
	select catx('=', strip(mnemonic), quote(compress(text, '', 'ka'))) into:labels separated by ' ' from have; 
quit;

proc transpose data=have out=have_wide;
	var text;
	id mnemonic;
run; 

/*Applying labels to all variables using macro variable "LABELS"*/
data want;
	set have_wide;
	label &labels;
run; 

proc print data=want label; 
	label; 
run; 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2655 views
  • 4 likes
  • 4 in conversation