Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted

## parsing a character of numbers that begins with 0 and keeping the initial zero

Hi,

I want to make a vector of values that come from a column in a datatable.  Some of those values start with the number zero.  When I use the parse command it truncates the zero from the beginning of the number.  The column is defined as a character column.  The script I am using is eval(parse(column(DTI, "bar_pen")[i])));  How can I do this and keep my initial zeros?

Thanks,

Tova

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: parsing a character of numbers that begins with 0 and keeping the initial zero

``````dt = New Table( "Untitled",
New Column( "Bar_Pen", Character, Set Values( {"0001", "9999", "0900"} ) )
);

list = dt:Bar_pen << getvalues;
show(list); // {"0001", "9999", "0900"};

v = dt:Bar_Pen;
show(v, parse(v), char(parse(v))); // "0900", 900, "900"
``````

You might not need to do the parse that converts the character data to code (which is just a number, most likely.) Then you don't need to eval the parsed code, then you don't need to convert the evaluated answer (a number) back to a string. If the values need cleaning up because of data entry errors, the eval/parse may seem to be working but will fail if a mischievous user puts bad code in the table.

Craige
3 REPLIES 3
Highlighted

## Re: parsing a character of numbers that begins with 0 and keeping the initial zero

this is what worked for me in the end:

Repeat("0", 4 - length(char(eval(parse(column(DTI, "Bar_Pen")[i]))))) || char(eval(parse(column(DTI, "Bar_Pen")[i])));

Highlighted

## Re: parsing a character of numbers that begins with 0 and keeping the initial zero

``````dt = New Table( "Untitled",
New Column( "Bar_Pen", Character, Set Values( {"0001", "9999", "0900"} ) )
);

list = dt:Bar_pen << getvalues;
show(list); // {"0001", "9999", "0900"};

v = dt:Bar_Pen;
show(v, parse(v), char(parse(v))); // "0900", 900, "900"
``````

You might not need to do the parse that converts the character data to code (which is just a number, most likely.) Then you don't need to eval the parsed code, then you don't need to convert the evaluated answer (a number) back to a string. If the values need cleaning up because of data entry errors, the eval/parse may seem to be working but will fail if a mischievous user puts bad code in the table.

Craige
Highlighted

## Re: parsing a character of numbers that begins with 0 and keeping the initial zero

thanks, that is a much neater solution than the one i found...

Article Labels

There are no labels assigned to this post.