BookmarkSubscribeRSS Feed
Ranjeeta
Pyrite | Level 9

Would someone be able to explain what is the difference in terms of the sas data step processing with the flaf 

I get different results with the same dataset 

data NAC_D_6; set NAC_D_5;

array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;

array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;



flag_stroke_q=1;



do i=1 to 10;

      if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]~='Q' and substr(op[i],1,4)~='I636'

      then flag_stroke_q=0;

end;

drop i;

if flag_stroke_q=0;



run;

data NAC_D_6; set NAC_D_5;
array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;
array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;

flag_stroke_q=0;

do i=1 to 10;
	if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]='Q' and substr(op[i],1,4)~='I636'
	then flag_stroke_q+1;
end;
drop i;
if flag_stroke_q=0;

run;*



data NAC_D_6; set NAC_D_5;

array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;

array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;

 

flag_stroke_q=0;

 

do i=1 to 10;

      if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]='Q' and substr(op[i],1,4)~='I636'

      then flag_stroke_q+1;

end;

drop i;

if flag_stroke_q=0;

 

run;*

2 REPLIES 2
ballardw
Super User

@Ranjeeta wrote:

Would someone be able to explain what is the difference in terms of the sas data step processing with the flaf 

I get different results with the same dataset 

data NAC_D_6; set NAC_D_5;

array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;

array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;



flag_stroke_q=1;



do i=1 to 10;

      if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]~='Q' and substr(op[i],1,4)~='I636'

      then flag_stroke_q=0;

end;

drop i;

if flag_stroke_q=0;



run;

data NAC_D_6; set NAC_D_5;
array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;
array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;

flag_stroke_q=0;

do i=1 to 10;
	if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]='Q' and substr(op[i],1,4)~='I636'
	then flag_stroke_q+1;
end;
drop i;
if flag_stroke_q=0;

run;*



data NAC_D_6; set NAC_D_5;

array op[10] Main_Problem OTHER_PROBLEM_1 - OTHER_PROBLEM_9;

array opp[10] MAIN_PROBLEM_PREFIX OTHER_PROBLEM_PREFIX_1 - OTHER_PROBLEM_PREFIX_9;

 

flag_stroke_q=0;

 

do i=1 to 10;

      if substr(op[i],1,3) in: ('I63' 'I64' 'H341') and opp[i]='Q' and substr(op[i],1,4)~='I636'

      then flag_stroke_q+1;

end;

drop i;

if flag_stroke_q=0;

 

run;*


When you use the code structure:

 

then flag_stroke_q+1;

Then you have told SAS the value of the  variable FLAG_STROKE_Q is to be retained across observatation so you should be careful with that structure.

Here's an example of that feature:

data example;
   set sashelp.class;
   flag+1;
run;

Not an issue because you initialize the variable in each iteration, but be careful.

 

You start with a different value for your Flag variable before the DO loop in both cases, one only  has the option of changing the flag to 0 and the other adds to the value of the each time a comparison is true.. So you create different values for the variable and then only keep the records when 0.

 

The first code would only have the record at least one of the conditions in the loop is true.

 

The second will keep the record only if ALL of the records have the comparisons as false.

Astounding
PROC Star

Well, you ran two different programs against the same data.  Is it a shock that the results are different?

 

Try removing this statement from both programs:

if flag_stroke_q=0;

Then you can look at the final values for FLAG_STROKE and see if they make sense to you.

 

On a side note, both programs get the wrong answer.  SUBSTR and IN: are usually a bad mix.  Remove the SUBSTR function.  Otherwise, you will process "H34" instead of "H341".

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 310 views
  • 0 likes
  • 3 in conversation