Test if any number follows an Alpha String

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Test if any number follows an Alpha String

Hello all. I am writing code to dynamically read in files saved by Analysts using Excel. The problem is that often files read in extra variables, which become formatted as VAR101, VAR102, etc...

I would like to be able to drop all var variables from the resulting proc contents (But do not drop them in the read in step). However it is also possible that a column name will have VAR within it, and I do not want these to be dropped.

Is it possible for sas to take a dataset as shown below

My variable

Var110

Var111

HeyVARmy

VARsarefun

and only flag the variables in which numerics follow the Var statement? I am looking for something similar to perl's [0-9] generic string search function so I can search "count(var,"var[1-9]")" in my data steps.

Thanks!


Accepted Solutions
Solution
‎01-15-2013 06:45 PM
PROC Star
Posts: 7,468

Re: Test if any number follows an Alpha String

Posted in reply to Anotherdream

Since SAS has regex functions, you could use the same ones you would use in PERL.  Conversely, not quite as good, you could do the following using a couple of non-regex SAS functions:

data have;

  informat My_variable $10.;

  input My_variable;

  cards;

Var110

Var111

HeyVARmy

VARsarefun

;

data test;

  set have;

  flag=ifn(upcase(substr(My_variable,1,3)) eq 'VAR' and

    not(missing(input(substr(My_variable,4), ?? 32.))),1,0);

run;

View solution in original post


All Replies
Solution
‎01-15-2013 06:45 PM
PROC Star
Posts: 7,468

Re: Test if any number follows an Alpha String

Posted in reply to Anotherdream

Since SAS has regex functions, you could use the same ones you would use in PERL.  Conversely, not quite as good, you could do the following using a couple of non-regex SAS functions:

data have;

  informat My_variable $10.;

  input My_variable;

  cards;

Var110

Var111

HeyVARmy

VARsarefun

;

data test;

  set have;

  flag=ifn(upcase(substr(My_variable,1,3)) eq 'VAR' and

    not(missing(input(substr(My_variable,4), ?? 32.))),1,0);

run;

PROC Star
Posts: 7,468

Re: Test if any number follows an Alpha String

Posted in reply to Anotherdream

I was hoping that someone more versed in regex functions would have provided an example.  The following will accomplish the task, but I'm sure it could be optimized (I'm still just learning regex expressions):

data want (keep=string variable);

  if _n_ = 1 then re = prxparse("/^var\d+$/i");

  retain re;

  input string $80.;

  length variable $32;

  call prxsubstr(re,strip(string),start,length);

  if start gt 0 then

    variable = substrn(string,start,length);

  datalines;

this

var1

variable5

something

Avar50

var85

Var888abc

VAR2012

Boston, VAR9999 MA 02116-7364

;

🔒 This topic is solved and locked.

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

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