BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
audreyliu201
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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');

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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');

audreyliu201
Fluorite | Level 6

Thank you. This is very clean and simple 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);
ChrisNZ
Tourmaline | Level 20

@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;

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 801 views
  • 4 likes
  • 3 in conversation