current working code:
data m2 m3;
set m1;
if substr(d_c_p1,1,3) = 'o9' or substr(d_c_p2,1,3) = 'o9' or substr(d_c_p3,1,3) = 'o9'
or substr(d_c1,1,3) = 'E8' or substr(d_c2,1,3) = 'E8'
or substr(d_c3,1,3) = 'E8' or substr(d_c4,1,3) = 'E8' or substr(d_c4,1,3) = 'E8' or substr(d_c5,1,3) = 'E8' or substr(d_c6,1,3) = 'E8'or substr(d_c7,1,3) = 'E8' then output m3;
else output m2;
run;
The one I modify but does not work so far.
array d_c_p{3}d_c_p1-d_c_p3;
do i=1 to 3;
if substr(d_c_p{i},1,3) = 'o9' then do output=m3; end;
array dx_code{7}d_c1-d_c7;
do j=1 to 7;
if substr(d_c{j}, 1,3)='E8' then do output=m3; end;
end;end;
else output m2;
the error message is:
: No matching IF-THEN clause
for the else output m2
please give advice how to correct thanks
Your first data step will output one record into M3 if any of the values match.
Your array approach is going to output the record each time one of the values matches, is that what you want?
Reformatting the code with the error to show the relationship between your do loops:
data m2 m3;
set m1;
array d_c_p{3}d_c_p1-d_c_p3;
array dx_code{7}d_c1-d_c7;
do i=1 to 3;
if substr(d_c_p{i},1,3) = 'o9' then do output=m3; end;
do j=1 to 7;
if substr(d_c{j}, 1,3)='E8' then do output=m3; end;
end;
end; /* would end the i loop*/
else output m2; /* this else is not associated with any IF.*/
run;
Provide an example of the input data, in the form of a data step. This links to instruction on a way to create a datastep from your M1 set: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
The provide the desired result.
You might be looking for something similar to this, untested
data m2 m3;
set m1;
array var {10} d_c_p1-d_c_p3 d_c1-d_c7;
array val {10} $ 2 _temporary_ ('o9','o9','o9','E8','E8','E8','E8','E8','E8','E8') ;
do i=1 to dim(var);
if substr(var{i},1,3) = val{i} then do;
output=m3;
Found=1;
leave ;
end;
end;
if missing(found) then output m2;
drop found;
run;
1. Missing a semi colon after DO.
2. Not an error but it's easier if you declare all arrays at top of code
3. Reference for second array should be dx_code not d_c(j)
4. When trying to output it's OUTPUT <dataset names(s)>, no equal sign
Ways to avoid these errors:
I didn't actually check if you have the correct do/end amounts or if the code makes sense, just the few things that a quick scan found. You may still have other errors, but you're on the correct path.
array d_c_p{3}d_c_p1-d_c_p3;
array dx_code{7}d_c1-d_c7;
do i=1 to 3;
if substr(d_c_p{i}, 1, 3)='o9' then
do;
output m3;
end;
do j=1 to 7;
if substr(dx_code(j), 1, 3)='E8' then
do;
output m3;
end;
end;
end;
else
output m2;
Thanks. I use your code
still the error message showed
else marked as red
no matching if-then clause
Count your do/end and make sure they match up.
EDIT: Show your log and trace your logic from that statement. Find the matching If for your last else condition.
I have four do and four end
the error marked :else as saying no matching if-then clause
output m3;
1330 end;
1331 end;
1332 END;
1333 else
----
160
ERROR 160-185: No matching IF-THEN clause.
1334 output M2;
1335
Your first data step will output one record into M3 if any of the values match.
Your array approach is going to output the record each time one of the values matches, is that what you want?
Reformatting the code with the error to show the relationship between your do loops:
data m2 m3;
set m1;
array d_c_p{3}d_c_p1-d_c_p3;
array dx_code{7}d_c1-d_c7;
do i=1 to 3;
if substr(d_c_p{i},1,3) = 'o9' then do output=m3; end;
do j=1 to 7;
if substr(d_c{j}, 1,3)='E8' then do output=m3; end;
end;
end; /* would end the i loop*/
else output m2; /* this else is not associated with any IF.*/
run;
Provide an example of the input data, in the form of a data step. This links to instruction on a way to create a datastep from your M1 set: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
The provide the desired result.
You might be looking for something similar to this, untested
data m2 m3;
set m1;
array var {10} d_c_p1-d_c_p3 d_c1-d_c7;
array val {10} $ 2 _temporary_ ('o9','o9','o9','E8','E8','E8','E8','E8','E8','E8') ;
do i=1 to dim(var);
if substr(var{i},1,3) = val{i} then do;
output=m3;
Found=1;
leave ;
end;
end;
if missing(found) then output m2;
drop found;
run;
in reality, , J=30 ( I need to repeat 30 times), i=3, so to use your code, seems to be a little bit time consuming
any other advice?
Why does it matter how many times it occurs?
@Bal23 wrote:
in reality, , J=30 ( I need to repeat 30 times), i=3, so to use your code, seems to be a little bit time consuming
any other advice?
If you are referring to my code with the temporary array being "time consuming" I think you have another issue. Adding another 20 variables and 20 values to the temporary array (in the correct order) as long as you are looking at the first 3 characters of each variable is a trivial amount of time compared to writing 30 if then else type statements and relatively easy to maintain.
It really helps if you explain what the code is trying to do.
I notice that the two blocks of code are doing different things. Is that on purpose?
Your first example outputs one copy of the record to either of two datasets.
Your second would (if it ran) output multiple copies of the record when there are multiple variables that match the condition.
EDIT:
To show an improved version of the earlier program. Here, after taking the 2-byte portions, put all together into a STRING- just taking not more than 50 bytes, using INDEX function we can search for the presence of '09' OR 'E8'. No need for 2 do-loops.
data m2 m3;
set m1;
length dcp1 dcp2 dcp3 dc1 dc2 dc3 dc4 dc5 dc6 dc7 $2;
length STR $50;
dcp1 = d_c_p1;
dcp2 = d_c_p2;
dcp3 = d_c_p3;
dc1 = d_c1;
dc2 = d_c2;
dc3 = d_c3;
dc4 = d_c4;
dc5 = d_c5;
dc6 = d_c6;
dc7 = d_c7;
call catx(' ',STR, dcp1,dcp2,dcp3,dc1,dc2,dc3,dc4,dc5,dc6,dc7);
match = (index(STR, '09') | index(STR, 'E8'));
if match then output m3;
else output m2;
run;
1. You are taking 3 characters and comparing with 2 characters. I presume that you want 2 characters.
2. Assigning to a character variable of length 2 with your string will be faster than the use of SUBSTR() function.
With these changes I placing the program below. This is not tested as your data set M1 is not known. Please test it and let the Community know about it.
data m2 m3;
set m1;
length dcp1 dcp2 dcp3 dc1 dc2 dc3 dc4 dc5 dc6 dc7 $3;
dcp1 = d_c_p1;
dcp2 = d_c_p2;
dcp3 = d_c_p3;
dc1 = d_c1;
dc2 = d_c2;
dc3 = d_c3;
dc4 = d_c4;
dc5 = d_c5;
dc6 = d_c6;
dc7 = d_c7;
do i = dcp1, dcp2, dcp3;
if i = '09' then do;
Match1 = 1;
leave; * Early match ;
end;
end;
do i = dc1, dc2, dc3, dc4, dc5, dc6, dc7;
if i = 'E8' then do;
Match2 = 1;
leave; *Early Match;
end;
end;
if Match1 or Match2 then output m3;
else output m2;
run;
Thanks for the effort. In fact, j=30, i=3, as I mentioned abover, so with your code, it generates a lot more variables, which take much time for later analysis. Any other tips.
I found i and j are used by ballardw.
I have a Question.
Comparing a substring of 3-characters with 2-character value('09', 'E8') will give wrong results. Did you check this?
Thanks. You are right. The value should be three but I typed wrong. I have used your code which works.
The fact is i have dcp1 to 3, and dc 1-30 instead of 7 that I previously posted. when i use you code, I have to type many times, adding more variables
that is why I am asking whether you have a better suggestion on this
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: