BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pritish069
Fluorite | Level 6

I want to compare 2 rows. i have 2 variables i and i1, i1 increments by 1 in do while loop and if then statement compare i and i1 compares and should give results result variable as 1 for whichever row is greater but i am not getting the results displayed.

 

Thank you in advance

 

data t;
infile datalines dlm="," dsd missover;
input i;
datalines;
2
3
4
5
;
run;

data t1;
set t;
i1=1;
do while (i le last.i);
if i < i1 then put result = 1;
else result = 0;
end;
i1 = i + 1;
run;

Output.JPG

1 ACCEPTED SOLUTION

Accepted Solutions
Satish_Parida
Lapis Lazuli | Level 10

Correction:

 

data t;
infile datalines dlm="," dsd missover;
input i;
datalines;
2
3
4
5
;
run;

data t1;
set t;
i1 = i + 1;
if i < i1 then /*put*/ result = 1;
else result = 0;
run;

View solution in original post

4 REPLIES 4
Satish_Parida
Lapis Lazuli | Level 10
data t;
infile datalines dlm="," dsd missover;
input i;
datalines;
2
3
4
5
;
run;

data t1;
set t;
i1 = i + 1;
if i < i1 then put result = 1;
else result = 0;
run;

/*This will work now. You do not need a loop to get increament, SAS 
Datastep it self is a loop, Advisable to use it always*/
pritish069
Fluorite | Level 6

Thank you for your advise but the result variable is still not giving output, i need the result variable to display 1 if the i1 is greater else display 0

 

Capture1.JPG

Satish_Parida
Lapis Lazuli | Level 10

Correction:

 

data t;
infile datalines dlm="," dsd missover;
input i;
datalines;
2
3
4
5
;
run;

data t1;
set t;
i1 = i + 1;
if i < i1 then /*put*/ result = 1;
else result = 0;
run;
ballardw
Super User

Is the variable i ever missing? Is i1 ever missing? If these can be yes you need to consider what result to you want for

i missing and i1 not missing

i not missing and i1 missing

both missing.

 

You should be aware that missing is less than any value (except missing) so you have the possibility of have a result that you really don't want. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 4 replies
  • 971 views
  • 1 like
  • 3 in conversation