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;

14 REPLIES 14
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:

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?

reneeharper
SAS Employee

I don't know how this happens. I can't reproduce the experience, so it is hard to debug.

I have been working with the Jive tech support staff.  They suggested that we verify the the post was opened as a question -- this one was. And that the person who is trying to mark it as helpful verify that they are actively logged in (refresh the page to make sure your log in hasn't expired).

The other option for @Csc to try is to select Reply on the answer they want to mark as correct.  It should also show up then.

I'd love to hear from you if either of these things work.

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
  • 14 replies
  • 20819 views
  • 4 likes
  • 10 in conversation