BookmarkSubscribeRSS Feed
newboy1218
Quartz | Level 8

Hi.. I am trying to come up with a SAS code that can identify all letter values among all variables. For example, I have a dataset that have 3 CHAR variables (var1 - var3):

 

var1    var2    var3

01       10        M9

02       3M       00

N                    09

07       18        11

 

How can I just extract all the letter values (values that have letter in them)?

var1    var2    var3

N         3M       M9

 

or, even better, how I can have output like this?

special_value

N

3M

M9

9 REPLIES 9
novinosrin
Tourmaline | Level 20
data have;
input (var1    var2    var3) ($);
cards;
01       10        M9
02       3M       00
N         .           09
07       18        11
;

data want;
set have;
array t var1-var3;
do i=1 to dim(t);
if anyalpha(t(i)) then
do;
want=t(i);
output;
end;
end;
keep want;
run;
newboy1218
Quartz | Level 8

Thanks for the quick reply!

 

If I have 100+ variables there, can I do this?

 

%let varlist = %str(var1 ... var1000); (?)




data
want; set have; array t &varlist.; do i=1 to dim(t); if anyalpha(t(i)) then do; want=t(i); output; end; end; keep want; run;

 

novinosrin
Tourmaline | Level 20

Sure.

 

I wonder why you need to quote though with %str

 

I believe just

 

%let arraylist=var1 var2 var3 varN;

 

should work fine

 

Macro quoting is not needed in this instance. 

 

 

newboy1218
Quartz | Level 8

Thank you! Let me try this! 😃

Jagadishkatam
Amethyst | Level 16

Alternatively with regular expressions

 

 

data want(where=(newvars ne ''));
set have;
array vars(*) $ var1 var2 var3; 
do i = 1 to dim(vars);
newvars=ifc(prxmatch('m/[A-Z]/',strip(vars(i))),strip(vars(i)),'') ;
output;
end;
run;
Thanks,
Jag
newboy1218
Quartz | Level 8

Thanks! 😃

Astounding
Opal | Level 21
If you truly have 100 variables wouldn't you also need to know which variable and which observation contains the character?
newboy1218
Quartz | Level 8

Sure.. how can I do that?

Astounding
Opal | Level 21

Let's build on your earlier program:

 

%let varlist = var1 ... var1000; 

data want;
  set have;
  array t &varlist.;
  do i=1 to dim(t);
     if anyalpha(t{i}) then do;
        want=t{i};
        recno = _n_;
        varname = vname(t{i}) ;
        output;
      end;
   end;
   keep want recno varname;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 580 views
  • 3 likes
  • 4 in conversation