BookmarkSubscribeRSS Feed
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

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.
2304 run;

 

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Like this? 🙂

 

/* 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;

 

novinosrin
Tourmaline | Level 20

Reason: _N_ is numeric variable and your $3. is character format ,So they are not matched

Try something like this:

data want1;
set sashelp.class;
id=left(_n_); /*or this*/
UniqueID=put(_N_,3.) ;/*remove the $ in the format*/
run;

 

Regards,

Naveen Srinivasan

Astounding
PROC Star

You might find it easier to work with if you insert leading zeros:

 

Unique_ID = put(_n_, z3.);

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 2973 views
  • 2 likes
  • 5 in conversation