DATA Step, Macro, Functions and more

basic but efficient programming for formatting.

Reply
Occasional Contributor
Posts: 12

basic but efficient programming for formatting.

Hi. It's Kate. 

 

I have a bunch of variables that are coded 0 for NO and 1 for YES.  I know how to change them using procformat to have labels, but when I have about 30 of those variables, is there a fast way to do them all in 1 macro or something? I want to keep the values, I just want to assign the labels. 

 

Thanks.  

 

For example:

 

death     drug A   tx1  trx2   drugb

0           1           1      1     1

1          0             0      1     1

 

I would like assign perm. labels for a whole bunch of those variables and tired of doing it for each one separately. 

thanks. 

 

 

 

 

Super User
Super User
Posts: 8,264

Re: basic but efficient programming for formatting.

Posted in reply to katemcguckin

@katemcguckin wrote:

Hi. It's Kate. 

 

I have a bunch of variables that are coded 0 for NO and 1 for YES.  I know how to change them using procformat to have labels, but when I have about 30 of those variables, is there a fast way to do them all in 1 macro or something? I want to keep the values, I just want to assign the labels. 

 

Thanks.  

 

For example:

 

death     drug A   tx1  trx2   drugb

0           1           1      1     1

1          0             0      1     1

 

I would like assign perm. labels for a whole bunch of those variables and tired of doing it for each one separately. 

thanks. 

 


I don't understand what you are complaining about.  You just need to define one format and attach it to all of the variables that need it.

proc format ;
  value yesno 1='Yes' 0='No';
run;
data want ;
 set have ;
 format death drug: tx: yesno.;
run;
Occasional Contributor
Posts: 12

Re: basic but efficient programming for formatting.

Hi,

 

I'm NOT complaining! I just wanted some help!

 

Thank you.  I figured it out. 

 

Your code worked, thank you.... Kate. 

 

Frequent Contributor
Posts: 112

Re: basic but efficient programming for formatting.

Posted in reply to katemcguckin

It's unclear from your post what you mean by "labels". The term "label" in SAS has a specific meaning: It's an attribute of a variable stored in the descriptor of a data file and serves to be surfaced instead of the variable's name if so requested. There're ways to assign and change labels; but since the FORMAT procedure isn't one of them, I guess you mean something else.

 

Perhaps by "labels" you mean formatted values like "Yes" and "No" that you want to see instead of 1 and 0. If so, simply assign the format where 1="Yes" and 0="No" to all your Boolean variables and make sure in your SAS session, SAS knows the path to the library where the compiled format is stored. If you store a data set with these variables and the format assigned to them, then any other data set you create from reading it will inherit the format attached to the variables. Ergo, you need to do the assignment only once. 

Super User
Posts: 10,530

Re: basic but efficient programming for formatting.

Posted in reply to katemcguckin

Your variables need to be "automatable" to make writing a format statement (as already noted, you are NOT talking about labels here!) easier.

Either all numeric variables in your dataset are boolean (in which case you can use the designator _numeric_), or they follow a naming pattern that allows use of wildcards (see @Tom's post). If neither is true, you need to write the format statement variable by variable on your own.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 4 replies
  • 75 views
  • 1 like
  • 4 in conversation