Regular Expression Matching in SAS

Go To StackoverFlow.com

2

I have observations that have 1 dollar signs ($), and observations that have 2 dollar signs ($). I want to assign different values to each type of observation. It should look something like this:

"$200 $300" gets assigned 1. "$200" gets assigned 2.

I've tried playing with Perl wildcards all day but no luck.

Thanks.

2012-04-05 17:12
by aesir


6

How about the SAS countc() function to count the number of times '$' appears?

%put %sysfunc(countc($300 $400,'$'));  /* returns 2 */
%put %sysfunc(countc($300 ,'$'));      /* returns 1 */
2012-04-05 17:56
by cmjohns
Crap. Forgot that SAS had all these fun super-specific functions. Yeah, that makes total sense - aesir 2012-04-05 18:01


3

You could use the goatse operator:

my @str = ( '$200 $300', '$200' );

foreach my $str ( @str ) {
    my $count =()= $str =~ /\$/g;
    print "count for $str is $count\n";
}

Then depending on what your $count is, assign the values accordingly.

2012-04-05 17:22
by CanSpice
+1 for making me look through the code until I found it : - user667489 2012-04-06 15:49
Ads