BookmarkSubscribeRSS Feed
Mumbai_1983
Calcite | Level 5

Hi,

 

I have a SAS dataset with over 100 variables .. My variable names are a_1, a_2 and so on, while the associated labels contain the variable name (Price, Sales and so on.

 

How do I get SAS to swap variable names and labels. I am only interested in label names, so if there is a way to get SAS to export labels instead of variable names, that will work for me too.


Please advice.  

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please give more concise examples of what you have, what you want as it is not clear.  Do you mean the order of the variables, or changing labels, or swapping data over?  If you want a list of variable labels look at SASHELP.VCOLUMN, has all the information for all the datasets in the SAS system.

Mumbai_1983
Calcite | Level 5

Sorry for not being clear, all I want to do is to replace variable names with labels

 

There are ways to do this manually but i have over 100 columns and don't want to manually do this.

 

So in my dataset, first variable has variable name a_1 and label is Price

second variable has variable name a_2 and label is Stock

 

My output dataset should have

 

first variable to be named Price

second variable to be named Stock

 

Is this clear?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah, ok yes thats clear.  I would strongly advise you not to do this, programming is far far easier with non-data labels.  Why would you want to do this anyway, the name of the variable is to be used in programming, the label is used when creating outputs, that is why there is two different parts, and what each one is used for.  Again, I would really advise you don't go down that route, unless there is a specific and very good reason, for example, you have created some variables and need to rename them to CDISC variables then that is ok, but you have fixed to and from's then.  If your just going to try to name the variable as per an ad hoc label, then you would need to account for: spaces, lengths too long for names, special characters or sets of characters etc.  Its just really not a good way of doing it.

AskoLötjönen
Quartz | Level 8

You can do this quite easily with two call execute loops, one for variable name and one for label. Here is example for renaming variables.

 

proc sql;
  create table names_labels as
  select memname,
         name,
         label
  from   dictionary.columns
  where  libname = 'LIBNAME'
     and memname = 'DATASET'
     and label ne '';
 quit;

 data _null_ ;
  set names_labels end=eof;
  if _N_ = 1 then do;
     call execute('data temp2.'||strip(memname)||';set temp.'||strip(memname)||'( rename = ( ');
  end;
  call execute (strip(name)||' = '||strip(label) );
  if eof then do;
     call execute (' );run;');
  end;
run;

 

add here second loop for labels.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, but:

label="Hello World" = fail

label="Hello_people's" = fail

label="Really_long_string_of_text_which_cant_be_a_variable_name" = fail

 

and programming:

array vars{n} hello_world hello_peoples really_long_string_of...; (*100)

or

array a_{n};

Which would you prefer?  As mentioned, there is a reason why there is a Name of Variable, and a Label of Variable.

FreelanceReinh
Jade | Level 19

I would prefer reasonably short, but meaningful variable names. This makes it easier to read and maintain the code.

 

Example: A formula like duration = enddt - startdt is less error-prone than a_73 = a_59 - a_58 (or was it a_58 - a_59??).

 

Even in an array, your original numbered variable names could be very confusing if the array indices did not match the numbers contained in the names.

 

Ideally, the names should be consistent, i.e. follow a naming convention (like "names of date variables end with DT, datetime variables with DTM, ...").


For the actual renaming I suggest a "semi-automated" process:

You can generate the list of "oldname = newname" assignments automatically, e.g. with a PROC SQL step like the following:

proc sql;
select cat(put(name, $6.), ' = ', strip(label)) /* adapt $6. if your names are longer! */
from dictionary.columns
where libname='MYLIB' & memname='HAVE'
order by name;
quit;

 

Then copy and paste the list from the output window into a data step (or a PROC DATASETS step, if you want to modify your original dataset in place):

data mylib.want;
set mylib.have;
rename
/* paste the "oldname=newname" list here */
;
run;


Finally, go through the pasted list manually and adapt the "newname" column to your naming convention. In particular, make sure that the new names are valid SAS names, not overly long and that there are no duplicates.

Reeza
Super User
I would also recommend against this. You can view the labels in output procs such as export print using a labels command.

data class;
set sashelp.class;
label age='Age (in years)'
weight='Weight (in kg)';
run;

proc print data=class (obs=5) noobs label;
run;

proc export data=class datafile='C:\_localdata\temp.xlsx' labels dbms=xlsx; run;

ballardw
Super User

@Mumbai_1983 wrote:

Hi,

 

I have a SAS dataset with over 100 variables .. My variable names are a_1, a_2 and so on, while the associated labels contain the variable name (Price, Sales and so on.

 

How do I get SAS to swap variable names and labels. I am only interested in label names, so if there is a way to get SAS to export labels instead of variable names, that will work for me too.


Please advice.  


Do you mean by "export" to export a SAS data set to another format such as CSV so that the Labels are column headers? If so something like:

proc export data=yourdata outfile="c:\path\youroutputfile.csv"

   dbms=CSV Label;

run;

 

Or to any ods destination:

 

proc print data=yourdata noobs label;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 11447 views
  • 0 likes
  • 6 in conversation