04-18-2017 03:10 PM
Why am I getting this error message? I need to create a new ID based on the _N_ observation variable. and I'd like the new variable to be a character variable. Is there a cleaner way to do this without the error message?
2300 data dataprep.comboIdentifier ;
2301 format UniqueID $3. ;
2302 set dataprep.comboIdentifier ;
2303 UniqueID=put(_N_,$3.) ;
WARNING: Variable _N_ has already been defined as numeric.
04-18-2017 03:16 PM - edited 04-18-2017 03:17 PM
/* Test Data Set */ data have; do i = 1 to 10; output; end; run; /* Use CATS function on _N_ to convert to character variable */ data want; length N $8.; set have; N = cats(_N_); run; /* Use PROC CONTENTS to verify that N is a character Variable */ proc contents data = want;run;
04-18-2017 03:20 PM - edited 04-18-2017 03:24 PM
Reason: _N_ is numeric variable and your $3. is character format ,So they are not matched
Try something like this:
id=left(_n_); /*or this*/
UniqueID=put(_N_,3.) ;/*remove the $ in the format*/
04-18-2017 03:27 PM
Your use of formats is a little confused. You need to use a numeric format with a numeric value like _N_. Also you are using a FORMAT statement where you probably meant to use a LENGTH or ATTRIB statement to define your new varaible. There is normally no need to attached $nn. formats to character varaibles. You probably do not want to have leading spaces in your new character variable. Also if you want your new character variable to sort in the same order as the original integer values then you should use the Z. format so that the smaller values will have leading zeros.
data dataprep.comboIdentifier ; length UniqueID $3 ; set dataprep.comboIdentifier ; UniqueID=put(_N_,Z3.) ; run;