data west;
set x;
IF no="04";
IF no="21";
IF no="23";
run;
There is a variable NO. I would like to keep no=04, no=21 and no=23 at the same time.
However, This code doesn't work.
Besides, I run it one by one and then set together. Is there any other way else to do it?
if NO = '04';
is the same as
if NO ne '04' then delete;
It means: only keep if NO='04' .
You want
if NO in ('04','21','23');
if NO = '04';
is the same as
if NO ne '04' then delete;
It means: only keep if NO='04' .
You want
if NO in ('04','21','23');
Thank you. This is very clean and simple
You would be better off storing those numeric values as numeric variables rather than text, unless there is a really good reason not to. For instance, calculations, where's, joins etc. all will work better with numerics, and if you say you need the 0 where only one digit is present, then use the z2. format. Best of both worlds then.
if no in (4,21,23);
@RW9 Good point. Another school of thought is that numeric variables should only be for values that can be used for calculations, and other values (like IDs) should be strings. Strings do need to use exact values ( '9' is not the same as ' 9' or '09' ) but this is not necessarily detrimental. I reckon the difference is just something to be aware of, not a reason to always use one or the other.
As for speed, strings can be be faster when merging.
data T1 T2; do I=1 to 1e8-1; output; end;
data _null_;
merge T1 T2; by I;
run;
*NOTE: DATA statement used (Total process time):
real time 27.15 seconds
user cpu time 25.14 seconds
system cpu time 2.01 seconds;
data T1 T2; do I=1 to 1e8-1; A=put(I,z8.);output; end; drop I;
data _null_;
merge T1 T2; by A;
run;
*NOTE: DATA statement used (Total process time):
real time 25.58 seconds
user cpu time 23.13 seconds
system cpu time 2.44 seconds;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.