Help using Base SAS procedures

value begining and ending with specific number

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

value begining and ending with specific number

[ Edited ]

Hi!  I have a variable (concurrentcrg)..and I need to flag all the observations with values that start with 5 but do not end in 1.

 

 

Here the line of code that did not work...

 

if concurrentcrg in: ('5') and concurrentcrg ne '%1' then status='Uncontrolled';

 

Help is appreciated!

 

 


Accepted Solutions
Solution
‎02-28-2017 12:37 PM
Super User
Posts: 17,785

Re: value begining and ending with specific number

If character field, look at the SUBSTR() function.

 

If numeric field look at the SUBSTRN() function.

The REVERSE() function will reverse a string so you can obtain the last character. Or you could use the LENGTH function to get the last character. 

 

Since it looks character, consider using the reverse function.

 

if substr(char, 1,1) = '5' and substr(trim(reverse(char)), 1,1) ne '1' then status='Uncontrolled';

View solution in original post


All Replies
PROC Star
Posts: 7,362

Re: value begining and ending with specific number

Is it a character or numeric field?

 

Art, CEO, AnalystFinder.com

Solution
‎02-28-2017 12:37 PM
Super User
Posts: 17,785

Re: value begining and ending with specific number

If character field, look at the SUBSTR() function.

 

If numeric field look at the SUBSTRN() function.

The REVERSE() function will reverse a string so you can obtain the last character. Or you could use the LENGTH function to get the last character. 

 

Since it looks character, consider using the reverse function.

 

if substr(char, 1,1) = '5' and substr(trim(reverse(char)), 1,1) ne '1' then status='Uncontrolled';
PROC Star
Posts: 7,362

Re: value begining and ending with specific number

Assuming it's character, then:

data want;
  set have;
  if first(concurrentcrg) eq '5' and first(reverse(strip(concurrentcrg))) ne '1'
    then status='Uncontrolled';
run;

Art, CEO, AnalystFinder.com

 

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 156 views
  • 1 like
  • 3 in conversation