BookmarkSubscribeRSS Feed
csc
Calcite | Level 5 csc
Calcite | Level 5


Is there an easier way?

data test;

Input textchar : $5.;

cards;

00100

00101

01010

10100

10101;

run;

data want;

set test;

new1 = tranwrd(textchar, "0", " ");   /* replace zeros with spaces */

new2 = substr(new1, verify(new1,' ')) /* remove spaces             */

new3 = tranwrd(new2    , " ", "0");   /* replace spaces with zeros */     

new4 = substr(new3, 1, length(new2)); /* remove trailing zeros     */

run;

16 REPLIES 16
art297
Opal | Level 21

data want;

  set test;

  want = tranwrd(strip(tranwrd(textchar, "0", " "))," ","0");

run;

Linlin
Lapis Lazuli | Level 10

data test;

Input textchar : $5.;

cards;

00100

00101

01010

10100

10101

;

run;

data temp;

length new $ 5;

  set test;

  n=put(input(textchar,5.),5.);

  new=ifc(substr(n,length(n),1) ne '0',n,substr(n,1,length(n)-length(scan(n,-1,"123456789"))));

drop n;

proc print;run;:smileysilly::smileysilly::smileysilly:

ishmo65
Obsidian | Level 7

You don't need to learn any non-SAS skills, like regular expressions, to solve this and you don't need reams of code. The easiest way to deal with the endings of strings is to use reverse with strip (or trim) and the eventual solution code will need less conditions/statements.

The "sample" input includes 00000 (mentioned in another reply).

data test;
input textchar : $5.;
cards;
00000
00100
00101
01010
10100
10101
;
run;

data want;
  set test;
  length want $5;
  want=textchar;
	do while (substr(strip(reverse(want)),1,1) eq "0");
		want=strip(reverse(substr(strip(reverse(want)),2)));
	end;
run;

proc print;run;

The result is:

Obs textchar want
1 00000  
2 00100 001
3 00101 00101
4 01010 0101
5 10100 101
6 10101 10101

Note: This is a solution based on my interpretation of the requirement.

 

 

The alternative "do" loop construct (still based on the above):

do until (substr(strip(reverse(want)),1,1) ne "0");

does not work - observations 3 and 6 are incorrect:

Obs textchar want
1 00000  
2 00100 001
3 00101 00101
4 01010 0101
5 10100 101
6 10101 10101

 

Michele_E76
Fluorite | Level 6

This method was SO much simpler than anything out there. I tried so many different variations but could not find something to remove the leading zeros and keep the other zeros in an account number....this was perfect! Thank you!

Haikuo
Onyx | Level 15

I don't know if this is easier, it is your call:

data test;

Input textchar : $5.;

cards;

00100

00101

01010

10100

10101

;

data want;

  set test;

  length want $5;

  want=prxchange("s/(0+$)//",-1,strip(prxchange("s/(^0+)//", -1,textchar)));

run;

proc print;run;

Haikuo

Astounding
PROC Star

Do we have an item for the SASWare Ballot here?

textchar = strip(textchar, '0');

Anybody else have a use for this type of functionality?

Note that some posted solutions work differently than others when the incoming string is 00000.

chang_y_chung_hotmail_com
Obsidian | Level 7

@Astounding: if so, then the strip would return "101", when the input was "10001," wouldn't it?


@Art: Thanks for the correction. I've got confused. Astounding and you are absolutely correct. strip(...) is equivalent to trimn(left(...))

art297
Opal | Level 21

Chang, No, strip only removes leading and trailing blanks (or, in this case, specific leading and trailing characters).

art297
Opal | Level 21

Put it in.  I'll vote for it!

Bal23
Lapis Lazuli | Level 10

I did try your code,

ERROR 72-185: The STRIP function call has too many arguments.

 

Patrick
Opal | Level 21

@Astounding

Put it in. I'll vote for it.

chang_y_chung_hotmail_com
Obsidian | Level 7

Another prx solution, handling the "00000" separately.

ods _all_ close;
ods listing;
options nocenter;
 
data test;
  Input textchar : $5.;
cards;
00100
00101
01010
10100
10101
11111
10001
00011
11000
00000
;
run;
 
data chang;
  set test;
  length new4 $5;
  if textchar = "00000" then
    new4 = "0";
  else
    new4 = prxchange("s|^0*(.*?)0*$|$1|", -1, textchar);
run;
 
proc print data=chang;
run;
/* on lst
Obs    textchar    new4
1      00100      1
2      00101      101
3      01010      101
4      10100      101
5      10101      10101
6      11111      11111
7      10001      10001
8      00011      11
9      11000      11
10     00000      0
*/

csc
Calcite | Level 5 csc
Calcite | Level 5

Thanks for all your input.  The reason it remains "Not Answered" is I have no icons to mark "Correct" or "Helpful".

art297
Opal | Level 21

You must have been logged in when you were on the page, but others have faced the same problem before and I don't think anyone has yet discovered why that happens sometimes.

: Renee, Do you know why people sometime don't get to see the correct and helpful icons when they do have a post marked as a question and are logged on?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 16 replies
  • 22880 views
  • 4 likes
  • 12 in conversation