DATA Step, Macro, Functions and more

in function for a list of strings within do loop

Reply
Contributor
Posts: 23

in function for a list of strings within do loop

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

 

 

 

Super User
Posts: 19,878

Re: in function for a list of strings within do loop

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.

Contributor
Posts: 23

Re: in function for a list of strings within do loop

Thank you for your reply!
SAS Super FREQ
Posts: 309

Re: in function for a list of strings within do loop

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;   
   



Frequent Contributor
Posts: 149

Re: in function for a list of strings within do loop

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

Contributor
Posts: 23

Re: in function for a list of strings within do loop

Posted in reply to error_prone
Thank you for your reply!
Super User
Posts: 19,878

Re: in function for a list of strings within do loop

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

Super User
Posts: 19,878

Re: in function for a list of strings within do loop

[ Edited ]

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 ...
Ask a Question
Discussion stats
  • 7 replies
  • 131 views
  • 2 likes
  • 4 in conversation