BookmarkSubscribeRSS Feed
Khalidhamad
Calcite | Level 5

I want to manipulate the dataset to get rid of the before and after character and then change it to numeric. the variable is like this Census Tract 1, Chaffee County, Colorado. and I just want to have the number and get rid of the rest and then change to numeric. Thank you

2 REPLIES 2
andreas_lds
Jade | Level 19

Please change the title of your topic to something meaningful.

You problems have been discussed so many times recently, i stopped counting. The compress-function can be used to remove unwanted chars, the input-function is the right tool to transform the result of compress into a number. But: you can't change the type of variable, you have to create a new one. Using the drop statement to get rid of unwanted variables should not be difficult.

Have a look at the documentation of the mentioned functions to learn their purpose and syntax.

ballardw
Super User

@Khalidhamad wrote:

I want to manipulate the dataset to get rid of the before and after character and then change it to numeric. the variable is like this Census Tract 1, Chaffee County, Colorado. and I just want to have the number and get rid of the rest and then change to numeric. Thank you


Actual examples of the values to manipulate and what the expected result should be in the form of data step code will get you a better response.

Unfortunately data values in the middle of narrative sentences open all sorts of questions about just where does your value start and end. Not really clear as such.

 

If you are not doing arithmetic with something identifiers seldom are better as numeric values.

 

data example;
   sometext='A value with numerals 1234 in the middle';
   justthenumerals = compress(sometext,,'dk');
   numericvalue = input(justthenumerals,6.);
run;

The above code 1) provides an example value, 2) uses the Compress function to Keep only Digits and then 3)converts the digits to a numeric value

Caution: This approach will only yield your apparent desire if there is only set of numerals in the string as this keeps ALL numerals.

Once a variable type is set you cannot change the type in SAS. So you need to create a new variable.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 621 views
  • 0 likes
  • 3 in conversation