DATA Step, Macro, Functions and more

Extract Number from a character variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Extract Number from a character variable

I imported data from CSV files and one of the variables is character (should be numeric).

 

The variables has numbers like "1000" and "2000" but some look like "1000..." or "2000...." - they have there extra "..." that vary in lenght at the end. How can I extract just the numbers?

 

Nadra


Accepted Solutions
Solution
‎11-30-2016 03:21 PM
Regular Contributor
Posts: 244

Re: Extract Number from a character variable

 

num_val = input(compress(char_val,,'kd'),best32.);

Now, I assume you have only positive integer here.  If decimal or negative are possible, then you may need much more complicated code to deal with that possibility (as you have . and such in your examples and can't just keep all . )...

View solution in original post


All Replies
Solution
‎11-30-2016 03:21 PM
Regular Contributor
Posts: 244

Re: Extract Number from a character variable

 

num_val = input(compress(char_val,,'kd'),best32.);

Now, I assume you have only positive integer here.  If decimal or negative are possible, then you may need much more complicated code to deal with that possibility (as you have . and such in your examples and can't just keep all . )...

Occasional Contributor
Posts: 8

Re: Extract Number from a character variable

Worked! Thanks!!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 195 views
  • 0 likes
  • 2 in conversation