Attribute statement

From sasCommunity
Jump to: navigation, search

The ATTRIB statement is used to specify the structure of the program data vector (PDV) in a DATA step, and thus the structure of any SAS data set(s) created in that step.

Except to the extent that earlier references to variables have already established PDV ordering, it can define the order in which the variables or columns appear and can be used to:

  • FAQ: change the order of variables
  • FAQ: reorder variables

Note: length should be the first element. Otherwise a character format or informat specification can preempt the determination of the character variable's length.

example:

attrib
VarChar length =$4   format  = <format name>.
                   informat  = <informat name>.
                   label     = 'label for VarChar'
                   transcode = yes|no
VarNumA length = 4   format  = <format name>.
                   informat  = <informat name>.
                   label     = 'label for numeric integer'
VarNumB length = 6   format  = <format name>.
                   informat  = <informat name>.
                   label     = 'label for numeric integer datetime'
VarNumC length = 8   format  = <format name>.
                   informat  = <informat name>.
                   label     = 'label for numeric real datetime with decimals'
;

Use this form to create an empty data set with a known structure which you can then use as base in Proc Append and avoid having to use the force option.

DATA Libref.DataStructure;
attrib ...;
stop;

The [documentation] appears to overstate the effect of ATTRIB, stating "Using the ATTRIB statement in the DATA step permanently associates attributes with variables by changing the descriptor information of the SAS data set that contains the variables.". In fact, one must use PROC DATASETS (or PROC SQL) to change descriptors. To demonstrate:

data test;
test = 'I am 18 in length.';
format test $18.;
run;
 
data _null_;
attrib test format = $7.;
set test;
run;
 
data _null_;
set test;
myformat = vformat(test);
put myformat= test=;
run;

If the ATTRIB statement had altered the data set header, the format in the last DATA step would be $7., whereas the PUT statement shows

myformat=$18. test=I am 18 in length.

References

-- created by User:Rjf2 27 October 2008, at 21:11.

-- editing and commentary by Howles and DonH

--Ronald_J._Fehd macro.maven == the radical programmer