BookmarkSubscribeRSS Feed
Yoko
Obsidian | Level 7

Hello, 

 

I try to simiply my program. 

I have this program and it works. 

=====================================================

data new_data;
set old_data;
array old_code[*] old_code1-old_code25;
array new_code[*] new_code1-new_code25;
do i = 1 to 25;
old_code[i]= compress(old_code[i], '.') ;
if old_code[i] in: ('E10','E11','E12','E13','E14') then new_code[i]=1;
else new_code[i]=0;
end;
output;

======================================================

I thought that I could replace  ('E10','E11','E12','E13','E14')  with ('E40':'E14').  

But I got an error message:

 

"ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant,
a datetime constant, a missing value, iterator, (, ), ','." 

 

In my real dataset, I have to list more like E10 to E90.  So, it would be nice if there is a way to make programming easy. 

 

I appreciate your suggestions. 

 

Oh, and for the above example, I understand that 'E10' following in: means any string starting with E10.  

Am I correct? 

 

Thank you, 

 

Yoko

 

 

 

7 REPLIES 7
Reeza
Super User

If you're criteria is matching the first three characters, then substr the first three characters and make your comparison. Unfortunately I think you're stuck typing it out because the list is character and ranges for characters aren't a good idea in my experience.

Yoko
Obsidian | Level 7
Thank you for your reply!
WarrenKuhfeld
Ammonite | Level 13

You can use a macro to generate the list.  The following step llustrates:


data _null_;
   %macro e; %do i = 10 %to 90; "E&i" %end; %mend; 
   x = 'E20';
   if x in (%e) then put 'Yes 20';
   x = 'E201';
   if x in (%e) then put 'no 201';
run;   
   



error_prone
Barite | Level 11

Can't test it now, but maybe creating a format helps.

Proc format;
   InValue narf
      'E10'-'E90' = 1
      Other = 0;
Run;


In the datastep: new_code[i] = input(old_code[i], narf.);

Yoko
Obsidian | Level 7
Thank you for your reply!
Reeza
Super User

@Yoko if your problem is solved please mark the correct solution as the answer/solution.

Reeza
Super User

Or, use some base functions - this would likely be considered old fashioned. I'm sure a PRX solution is better but I avoid those like the plague. 

 

1. Check if first character is E

2. Check if next two characters are in 10:90

 

 

if char(character_variable, 1) = 'E' and input(substr(character_variable, 2, 2), 8.) in (10:90) then do ...

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2696 views
  • 2 likes
  • 4 in conversation