BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PatrykSAS
Obsidian | Level 7

Hello, I came across the following problem in the example data:

ID
003
0101
150
00070
My case is to get ID without any '0' only at the beginning, so my result should be:
ID
3
101
150
70
How can I achieve that? Have a nice day 🙂
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @PatrykSAS,

 

The VERIFY function would avoid potential length issues:

data have;
input id $;
cards;
003
0101
150
00070
;

data want;
set have;
id=substr(id,verify(id,'0'));
run; 

View solution in original post

7 REPLIES 7
ballardw
Super User

I would start with something like:

data want;
   set have;
   id = put(input(id,best.),best. -L);
run;

which may have some dependencies on length of values.

 

Next I would examine where the data came from, why it is in an undesired format, and read into SAS as needed.

 

BTW, if this is force values to match another NUMERIC value created with a different read/import then you have other issues because this is a Character value.

Shmuel
Garnet | Level 18

I suppose the ID is defined as char type variable.

then convert it to numeric by:

data want;
set have(rename=(ID=_ID);
     id = input(_ID, best5.);
    format ID 5.;
run;
FreelanceReinh
Jade | Level 19

Hello @PatrykSAS,

 

The VERIFY function would avoid potential length issues:

data have;
input id $;
cards;
003
0101
150
00070
;

data want;
set have;
id=substr(id,verify(id,'0'));
run; 
JackHamilton
Lapis Lazuli | Level 10

The prxchange function is another possibility for character strings:

 

data want;
    set have;
    newid = prxchange('s/^(0*)(.*)/$2/', 1, id);
run; 
PatrykSAS
Obsidian | Level 7
THANK YOU
Ksharp
Super User
data have;
input id $;
cards;
003
0101
150
00070
;

data want;
set have;
new_id=prxchange('s/^0+//',1,id);
run; 
reprui
SAS Employee
I know this is already  "solved," but wanted to illustrate that there are many ways to solve a data problem with SAS... 🙂
 
/*************/
/*Anotherway1 "For Grins!" If data "does not" already exist...Create as numeric...*/
data AnotherWay1;
input id 8.;
cards;
003
0101
150
00070
;
Run;
/*****************/
/*****************/
data have;
input id $;
cards;
003
0101
150
00070
;
Run;
/*Anotherway2 "For Grins!" If "have" already exists...*/
data AnotherWay2 (drop=id_num);
length id_num 8.;
set have;
id_num=id;
id=strip(id_num);  *<- in case there are padded blanks...;
run;
/***************/

 


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2057 views
  • 4 likes
  • 7 in conversation