DATA Step, Macro, Functions and more

do loop

Reply
SAS Employee
Posts: 1

do loop

I would like to do infinity loop like in JAVA

 

for(i=1;i<200;I++){


       if (COL(i) != COL1 AND COL(i) != "  ") {

                 q_person_type = "Multiple";

       }

       else {

                q_person_type = COL1

        }                

}

 

For SAS Code

 

data test3(keep=q_full_nm q_person_type rename=(q_full_nm=node_tooltip));
    set test2;
    do i = 1 to n by 1;
       q_person_type=COL1;
       if COLi ne ' ' AND COLi ne COL1 then q_person_type="Multiple";
run;

 

I cannot run this code, could you please check it which part i'm wrong

what i want is if COL2, COL3 ... until COL N is not equal to COL1 return "Multiple" but if COL2, COL3 .... until COL N is equal to COL1 or is "  " return COL1

 

Please see picture for more understanding


pic.JPG
Super User
Posts: 5,257

Re: do loop

You need to fill an array with your col2 to coln variables, and loop over that.
Data never sleeps
Super User
Super User
Posts: 7,407

Re: do loop

Hi,

 

You can use arrays to loop over data, example below.  Please note this code is not tested.  If you want working code, please post test data - in the form of a datastep, which reflects your actual data - and what you want the output to look like.

data want;
  set have;
  array col{3};
  result="Pass";
  do i=2 to 3;
    if col{i} ne col{1} then result="Fail";
  end;
run;
Respected Advisor
Posts: 3,777

Re: do loop

[ Edited ]

I looked at the picture of your data and wrote this.  Perhaps with this and if you provide and explanation of what you need someone will be able to help.

data col;
   infile cards missover;
   input id:$3. (col1-col3)(:$2.);
   cards;
001 TP
002 TP 
003 RP
004 TP
005 TP
006 RP TP
007 RP
008 RP TP
009 RP
010 RP
011 TP RP RP
;;;;
   run;
proc print;
   run;

Capture.PNG

Ask a Question
Discussion stats
  • 3 replies
  • 216 views
  • 0 likes
  • 4 in conversation