BookmarkSubscribeRSS Feed
Chuckles
Calcite | Level 5

Kinda of stumped with this one so hoping the community can help. In the list of values below. 

If the id starts with '66' (not all id's start with 66) the leading 6 should be removed from the value so the value is only 17 characters long.  Not all id's are 17 characters long.

 

Before processing 

id,Zip,registered
660004583468247345,28211,Y
660015613515135135,45286,Y
660351531581351551,72458,N
660044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y

 

What the end result shoudl look like

id,Zip,registered
60004583468247345,28211,Y
60015613515135135,45286,Y
60351531581351551,72458,N
60044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y

 

Any tips on how to write the code for this?

3 REPLIES 3
ballardw
Super User

One way, first creating an actual data set.

data have;
   infile datalines dlm=',';
   input id :$20. Zip :$5. registered :$1.;
datalines;
660004583468247345,28211,Y
660015613515135135,45286,Y
660351531581351551,72458,N
660044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y
;

data want;
  set have;
  if id=:'66' then id=substr(id,2);
run;

The =: is a "begins with". Substr says to start at the second character to length of the value and keep that.

 

Note the data step is the best way to share data. That way we do not have to make guesses as to variable types.

You really did not need to include other variables if they are not involved in the problem.

 


@Chuckles wrote:

Kinda of stumped with this one so hoping the community can help. In the list of values below. 

If the id starts with '66' (not all id's start with 66) the leading 6 should be removed from the value so the value is only 17 characters long.  Not all id's are 17 characters long.

 

Before processing 

id,Zip,registered
660004583468247345,28211,Y
660015613515135135,45286,Y
660351531581351551,72458,N
660044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y

 

What the end result shoudl look like

id,Zip,registered
60004583468247345,28211,Y
60015613515135135,45286,Y
60351531581351551,72458,N
60044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y

 

Any tips on how to write the code for this?


 

Chuckles
Calcite | Level 5

Thank you so much Ballardw,

 

Was not aware of The =: is a "begins with". Substr  (glad I know this now for future reference).

 

Will format my questions better going forward so it is easier to understand.

Ksharp
Super User
data have;
   infile datalines dlm=',';
   input id :$20. Zip :$5. registered :$1.;
datalines;
660004583468247345,28211,Y
660015613515135135,45286,Y
660351531581351551,72458,N
660044415151535151,106957,Y
1358153,41763,Y
10154543,95847,Y
10071,8547,Y
1000586,65482,Y
10442151,54368,N
8000397,47352,Y
;

data want;
  set have;
  id=prxchange('s/^6+/6/',1,id);;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 567 views
  • 0 likes
  • 3 in conversation