Candice Sessa
Candice Sessa

Reputation: 57

How to loop through a list of string values in SAS

I am working on a project where I have a list of 45 codes. My goal is to find all codes that start with the first 3 characters of the code.

For example, I need to find all codes that start with N31, N32,N33....N34. I have figured out how to do this manually in SAS (use a where code like N31_ or code like N32_)

data slice;
set dat.all_hcpcs_nitos;

where nitos like "N30_"
or nitos like "N31_";
run;

I am not looking forward to hard coding this 45 times and am hoping to find something...more elegant. If I were in python, I would make a list of the 45 codes then iterate through each one in the list. I am unsure how to do this type of process in SAS.

From my searching it looks like I may need to do a %let codes = "list of codes". I have also tried doing something like i = 1 to 8 and try to do a search like:

data slice;
set dat.all_hcpcs_nitos;

%do i = 1 to 8;
where nitos like "N3&i._";
%end;
run;

Thank you in advance for any suggestions. I have been challenging myself on making my code more compact, and the SAS macros always look confusing to me.

Upvotes: 0

Views: 1576

Answers (1)

Tom
Tom

Reputation: 51566

Just use the IN operator with the : modifier to have it do a trimmed comparison. Example:

proc print data=sashelp.class;
  where name in: ('Ali','Bar');
run;

Upvotes: 2

Related Questions