BookmarkSubscribeRSS Feed
Denali
Quartz | Level 8

Hi,

 

I have a ID column is currently numeric, and I would like to change it to character. I have a hundred ID numbers 1-100 needs to be 5 numbers beginning with zero, e.g. 00001, 00002, 00003,.....,00099, 00100. Then another twenty ID numbers just need to be 4 numbers, beginning with two, e.g. 2000, 2001, 2002, 2003, ......2020. 

 

ID

00001

00002

00003

 

00099

00100

2000

2001

2002

 

2019

2020

 

How do I change the format and designate the length for these two conditions? Thank you!

6 REPLIES 6
Patrick
Opal | Level 21

I'd be adding leading zero's to all of your ID's (using format z5. only) so sorting behaves as you'd likely would want it - but here you go:

data have;
  input id ;
  datalines;
00001
00002
00003
00099
00100
2000
2001
2002
2019
2020
;
run;

proc format;
  value conv_id (default=5)
    0-100  = [z5.]
    other = [f5.]
  ;
quit;


data have(drop=id_orig);
  set have(rename=(id=id_orig));
  length id $5;
  id=put(id_orig,conv_id. -l);
run;
Denali
Quartz | Level 8

Hi Patrick,

 

Thank you for the code. It worked!

 

I have another question: How do I reorder the ID variable back to the first column. It is now listing in the last column.

 

Thanks again!

Patrick
Opal | Level 21

@Denali wrote:

Hi Patrick,

 

Thank you for the code. It worked!

 

I have another question: How do I reorder the ID variable back to the first column. It is now listing in the last column.

 

Thanks again!


The variable order in a table shouldn't be of real relevance but... you can have the ID column as first column simply by placing the LENGTH statement before the SET statement. This way the ID column gets defined first and though will be the first column in the new Have table.

data have(drop=id_orig);
  length id $5;
  set have(rename=(id=id_orig));
  id=put(id_orig,conv_id. -l);
run;

 

Denali
Quartz | Level 8

Thank you Patrick. 

 

Now I've got another dataset, and the ID column is character, not numeric. We still want the #1-100 to be length 5 and >=2000 to be length 4, just like below. I do I code it? 

 

ID

00001

00002

00003

 

00099

00100

2000

2001

2002

 

2019

2020

 

Thank you!

Patrick
Opal | Level 21

@Denali 

Ideally create new questions as a new discussion and reference the old discussion there if it's related.

 

The Zw. format which so nicely lets you add leading zero's is for numerical variables. If your source is a character variable containing digits only then easiest is to first convert the character to numeric and then apply the Zw. format. Based on the code I've posted earlier this could look as below:

id=put(input(id_orig,best32.),conv_id. -l);
andreas_lds
Jade | Level 19
Please post example data showing what you have.

This creates a char-version with leading zeros:
length char_id $ 5;
char_id = put(id, z5.);

You have to explain how the observations that should start with two can be identified.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 835 views
  • 2 likes
  • 3 in conversation