Hey Guys,
this question was probably already asked before but I am not only new to SAS, but also new to this forum, so please bear with me.
I have a dataset where I wanted to sort a character variable with length 18, although it is a character variable it only contains numbers (IDs). If I try to sort this variable, I get an error because in at least one observation there are letters mixed into the ID.
How do I find out how many observations contain letters, I think I have to use the anyalpha command, but not quite sure how to apply it.
Thanks in advance!
Can you use sortseq=linguistic (numeric_collation=on) in your proc sort?
Anyhows:
number_of_chars=lengthn(compress(id," ","ka"));
Pop that in a datastep and it will tell you how many alphas are in the id string. ka = keep alpha.
Hi,
To find the number of observations, you could try proc sql as in the following example:
/* set up an id variable with some containing alphabetic characters */
data have;
set sashelp.class;
length id $ 8;
/* add alphabetics to id if name begins with 'J' */
if name eq: 'J' then
id = cats('ABC',id);
else
id = cats(_n_);
run;
%let id_alpha = 0;
/* save the count of id's with alphabetic characters into a macro variable */
proc sql noprint;
select
count (*)
into
:id_alpha
from
have(keep = id)
where
anyalpha(id)
;
quit;
%put id_alpha = &id_alpha;
Regards,
Amir.
Cheers Guys!
Tried the methods and both work well and deliver the same result, thank you!
Anyalpha is used to find the first occurrence of an alphabet in a string.
data want;
set have;
retain cnt 0;;
Num_of_Chars = lengthn(compress(ID,,'d')); *If you want to include special characters(if exist);
Num_of_Chars = lengthn(compress(ID,,'ka')); *If you have only alphabets and digits;
if Num_of_Chars gt 0 then cnt=cnt+1;
call symput("No_Of_Obs",cnt);
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.