DATA Step, Macro, Functions and more

how to remove the first numbers from a character var, including numbers and "."

Accepted Solution Solved
Reply
Super Contributor
Posts: 336
Accepted Solution

how to remove the first numbers from a character var, including numbers and "."

2.1 Child Conditions
2.2 Teenage Conditions
 2.3 Adult Conditions
2.4 Senior Conditions

I need to modify this variable, by deleting 2.1, 2.2 etc. and keep Child conditions etc. The lengths of the words left are not same.

 

The desired var2 should be

Child conditions

teenage conditions

adult conditions

senior conditions

 

Compress can only remove ., any advice how to remove the first numbers?

Thanks.


Accepted Solutions
Solution
‎08-02-2016 12:58 PM
Super User
Posts: 10,497

Re: how to remove the first numbers from a character var, including numbers and "."

Assuming you do not have spaces before the numbers in question

 

Variable = substr(variable,index(variable,' ')+1);

 

might work.

View solution in original post


All Replies
Solution
‎08-02-2016 12:58 PM
Super User
Posts: 10,497

Re: how to remove the first numbers from a character var, including numbers and "."

Assuming you do not have spaces before the numbers in question

 

Variable = substr(variable,index(variable,' ')+1);

 

might work.

Super Contributor
Posts: 336

Re: how to remove the first numbers from a character var, including numbers and "."

Thanks

SAS Employee
Posts: 104

Re: how to remove the first numbers from a character var, including numbers and "."

[ Edited ]

Actually, COMPRESS with the appropriate modifiers could handle this particular problem very nicely :-) The third argument accepts modifiers to change the behavior of COMPRESS. In this case, I'd use 'k' for KEEP (instead of the default REMOVE behavior) and add an 'a' to specify ALPHABETIC characters along with 's' to specify SPACE characters. The STRIP function gets rid of any leading spaces.

 

The result:

data test;

   length Original Fixed $ 30;

   infile datalines dsd;

   input Original;

   Fixed=strip(compress(original,,'kas'));

   put original=$quote. Fixed=$quote.;

datalines;

2.1 Child Conditions

2.2 Teenage Conditions

2.3 Adult Conditions

2.4 Senior Conditions

;

 

Result:

 

Obs Original Fixed
1 2.1 Child Conditions Child Conditions
2 2.2 Teenage Conditions Teenage Conditions
3 2.3 Adult Conditions Adult Conditions
4 2.4 Senior Conditions Senior Conditions
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 617 views
  • 2 likes
  • 3 in conversation