DATA Step, Macro, Functions and more

Can a variable inherit attributes?

Reply
Contributor
Posts: 62

Can a variable inherit attributes?

Hi,

 

I have a file containing responses to a questionnaire, and want to assign the values of some of the variables to new variables, so that I can make use of a macro (more details here: https://medicine.umich.edu/dept/urology/research/epic).

 

Everything works well, except that the new variables don't inherit the labels from the old variables, which is slightly annoying. Is there a way to make this work?

 

Just to spell it out:

attrib SC_EPICB_EPICBQ1 length=$2 label='1. Rectal Urgency';
...
EPICBQ1 = SC_EPICB_EPICBQ1 ;
...
q42 = input(EPICBQ1, 1.);
...

Norman.
SAS 9.4 (TS1M0) X64_7PRO WIN 6.1.7601 Service Pack 1 Workstation

Super User
Posts: 6,938

Re: Can a variable inherit attributes?

Use the same attrib statement that you used for the original variable. If you want to do it automatically, get the attributes from sashelp.vcolumn and create the attrib statement(s) into a macro variable.

It all depends on what you finally want to achieve.

 

Automatic assignment of attributes makes no sense, once you think through it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,083

Re: Can a variable inherit attributes?

While you can use macro language and dictionary.columns to pick out all the attributes, it's probably easier to make the new variables actually be the old variables.  For example:

 

data new_variables;

set have;

keep id sc_dpicb_epicbq1;

rename sc_dpicb_epicbq1 = epicbq1;

run;

 

data want;

merge have new_variables;

by id;

run;

 

Of course, the first DATA step can keep and rename many more new variables in a single step.

Super User
Posts: 10,500

Re: Can a variable inherit attributes?


Norman21 wrote:
Everything works well, except that the new variables don't inherit the labels from the old variables, which is slightly annoying. Is there a way to make this work?
I am not sure why you would expect new variable to inherit from existing as they are new hence different. If you name a new variable of the year value from a date variable would you want it to inherit the format for a date value? A year value of 2016 that inherits a date format is going to be displayed as 1967. Inheriting the label would be very misleading. And if I make a new variable by concatenating four 2-character variables I would likely most often not want a 2 character length or format.
I recode lots of survey variables and often name the recoded from Q1 to RQ1. With the label and formats it is usually easiy enough to copy and paste the original label and formats and then use editor search and replace on a selection of code to change to the new variables.
I hope there is much more going on with your variables because when all you show is creating a new variable and then using INput to change type it raises a question about why not just to the input directly, or even when reading the original data the first time.
Ask a Question
Discussion stats
  • 3 replies
  • 242 views
  • 4 likes
  • 4 in conversation