<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Working in multiple loops in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450633#M283739</link>
    <description>&lt;P&gt;It is not a good idea because you in a datastep will overwrite the newval. Datastep change all collumn(newval) every time in each loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code must to be sequential&amp;nbsp;from&amp;nbsp;first observation to last observation.&amp;nbsp;That's is the idea of two loops( 2 search )&amp;nbsp;with do while statement.&amp;nbsp;One for search what i want to update (newval =1) and another intern loop to search the other observation previous to compare and for example calculate the age different.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Apr 2018 13:47:33 GMT</pubDate>
    <dc:creator>Aleixo</dc:creator>
    <dc:date>2018-04-03T13:47:33Z</dc:date>
    <item>
      <title>Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450563#M283733</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with sashelp.class to understand well the loops in a dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My idea is to find the name Alfred and check if is Robert in a previous observations. The same thing for when observations is Robert find if Ronald is a previous observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If&amp;nbsp;I check this i will put a new variable&amp;nbsp;value=1 if not value=0. The problem is in save the actual observations and it is in a infinite loop. I don't know why because de code is idented.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if the result is correct because i don't have the output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loopOverDatasets();

    %local datasetCount iter inter2 inName inName2;
    %let Name1 = 'Alfred';&lt;BR /&gt;    %let Name2 = 'Robert';
    %let Name3 = 'Ronald';
    proc sql noprint;
        select count(*)
        into :datasetCount
        from sashelp.class;
    quit;
    %let iter=1;
    %do %while (&amp;amp;iter.&amp;lt;= &amp;amp;datasetCount.);
        data _NULL_;
            set sashelp.class (firstobs=&amp;amp;iter. obs=&amp;amp;iter.);
            call symput('inName',strip(Name));
        run;
		data Group;
			set sashelp.class;
			iter2 = 1; 
        		%if (&amp;amp;inName. EQ &amp;amp;Name1.) 
					%then %do;
						%do %while (&amp;amp;iter2. &amp;lt;= &amp;amp;iter.);
							data _NULL_;
            					set sashelp.class (firstobs=&amp;amp;iter2. obs=&amp;amp;iter2.);
            					call symput('inName2',strip(Name));
       						run;
							%if (&amp;amp;inName. EQ &amp;amp;Name2.) 
								%then %do;
									value=1;
							%end;
							%else  
								value=0;
							%end;
							%let iter2=%eval(&amp;amp;iter2.+1);
						%end;
				%end;
				%if (&amp;amp;inName. EQ &amp;amp;Name2.) 
					%then %do;
						%do %while (&amp;amp;iter2. &amp;lt;= &amp;amp;iter.);
							data _NULL_;
            					set sashelp.class (firstobs=&amp;amp;iter2. obs=&amp;amp;iter2.);
            					call symput('inName2',strip(Name));
       						run;
							%if (&amp;amp;inName. EQ &amp;amp;Name3.) 
								%then %do;
									value=1;
							%end;
							%else 
								value=0;
							%end;
							%let iter2=%eval(&amp;amp;iter2.+1);
						%end;
				%end;
		run;
        %let iter=%eval(&amp;amp;iter.+1);
    %end;
%mend;

%loopOverDatasets()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Someone can help me&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Aleixo&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 10:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450563#M283733</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-03T10:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450568#M283734</link>
      <description>&lt;P&gt;Why so complicated?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let actval=Ronald;
%let prevval=Alfred;

data want;
set sashelp.class;
retain flag 0;
if name = "&amp;amp;prevval." then flag = 1;
if name = "&amp;amp;actval." and flag
then newval = 1;
else newval = 0;
drop flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As usual, a macro is not needed.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 10:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450568#M283734</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T10:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450570#M283735</link>
      <description>&lt;P&gt;You are not using dataset loops there at all as far as I can tell, it is all macro code.&amp;nbsp; Now based on the knowledge that macro is never needed, and you should always try to use Base SAS code, I would ask why you have written this big chunk of code.&amp;nbsp; I can see no reason for it, as a simple retain statement in a datastep will do the requested logic.&amp;nbsp; Provide some test data - in the form of a datastep - so that we have something to write code to, and show what the output should look like.&amp;nbsp; We can then provide you with some simple code to achieve your task.&amp;nbsp; If you do not know how to get a datastep of your data follow this post:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, do not the code you have written is invalid in several ways - starting with the obvious data _null_ steps within another datastep (data group) which is invalid.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 10:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450570#M283735</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-03T10:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450572#M283736</link>
      <description>&lt;P&gt;Note this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	data Group;
			set sashelp.class;
			iter2 = 1; 
        		%if (&amp;amp;inName. EQ &amp;amp;Name1.) 
					%then %do;
						%do %while (&amp;amp;iter2. &amp;lt;= &amp;amp;iter.);
							data _NULL_;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When SAS encounters the data _NULL_ statement, which constitutes a step boundary, the first data step will look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Group;
set sashelp.class;
iter2 = 1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and start to execute, so you get a copy of sashelp.class with column iter2 added where all values are 1.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 11:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450572#M283736</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T11:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450620#M283737</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for your answer. Worked fine.&lt;/P&gt;&lt;P&gt;It is not soo simple xD. What i ask is simple because&amp;nbsp;i am at start&amp;nbsp;but i can complicate a little bit. My background in programming is thinking that way. Now SAS is a little different.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my idea these names will be dinamic, not always the same name. Other idea is If my list has more than one Alfreds or Ronalds i will be check the names and another variables like the age.&amp;nbsp; My code try to thinks in all cases that i want to check. That's why is so complicated. The newvalue is a flat to check all obervations with the condition "name".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How i can calculate newval = Age(Alfred) - Age(Ronald)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That's why i need to save observations and search after search until the end.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry about all my questions, i try to search on community, lots of topics but the codes are only for one simple thing and i have to mix them to achieve my goal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Aleixo&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 13:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450620#M283737</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-03T13:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450623#M283738</link>
      <description>&lt;P&gt;Well, the basic logic stays the same: retain a reference variable and compare current values to it.&lt;/P&gt;
&lt;P&gt;If you need to run the test repeatedly with lots of different values, wrap the code into a macro (that's why I used two macro variables, set them as macro parameters), and then use call execute() to call the macro off a dataset of values.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 13:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450623#M283738</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T13:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450633#M283739</link>
      <description>&lt;P&gt;It is not a good idea because you in a datastep will overwrite the newval. Datastep change all collumn(newval) every time in each loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code must to be sequential&amp;nbsp;from&amp;nbsp;first observation to last observation.&amp;nbsp;That's is the idea of two loops( 2 search )&amp;nbsp;with do while statement.&amp;nbsp;One for search what i want to update (newval =1) and another intern loop to search the other observation previous to compare and for example calculate the age different.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 13:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450633#M283739</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-03T13:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450666#M283740</link>
      <description>&lt;P&gt;I can't make any sense of this. Please provide some example data (&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;in a data step&lt;/A&gt;) and an example for the expected output, with comments for why what is expected.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 14:38:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450666#M283740</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T14:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450674#M283741</link>
      <description>&lt;P&gt;SQL is a likely candidate for joining data sets on different criteria especially if place the records into a data set.&lt;/P&gt;
&lt;P&gt;No claim that this is the most efficient way but is one way to build a moderately complex comparison of records based on different values in a single data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.names;
   length  firstname secondname $ 8;
   input firstname $ secondname $;
datalines;
Alfred  Thomas
Barbara Judy
;


proc sql;
   create table want as
   select firstset.firstname, secondset.secondname, firstset.age as FirstAge,
          secondset.age as SecondAge, (firstset.age - secondset.age) as AgeDif
   from (select a.firstname,a.secondname, b.* from work.names as a
                    left join
                    sashelp.class as b
                    on a.firstname=b.name ) as firstset
        left join
        (select c.firstname,c.secondname, d.* from work.names as c
                    left join
                    sashelp.class as d
                    on c.secondname=d.name ) as secondset
       on firstset.firstname=secondset.firstname
       and firstset.secondname=secondset.secondname
   ;
quit; 
&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Apr 2018 14:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450674#M283741</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-03T14:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450737#M283742</link>
      <description>&lt;P&gt;Ok. Thank you all answers, seriously. I think the solutions are not what i want. I will be more specific:&lt;/P&gt;&lt;P&gt;My data in a dataset that i have to try the code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.dados(label='Dados');
  infile datalines dsd truncover;
  input Time:32. Name:$8. Tp:$1. Age:$3. Type:$2.;
datalines4;
107,Joyce,B,OFF,Dj&lt;BR /&gt;110,Joyce,B,OFF,Dj
202,Jane,C,OFF,Dj
304,Alice,A,ON,Pr
476,Alice,A,OFF,Dj
546,Barbara,D,OFF,Dj
628,Carol,F,OFF,Dj
756,Mary,G,ON,Pr
858,Mary,F,ON,Pr
929,Mary,G,OFF,Dj
949,Thomas,I,ON,Pr
989,Thomas,I,OFF,Dj
1002,Jane,C,ON,Pr
1006,Jane,C,ON,Pr
1010,Jane,Z,ON,Pr
1100,Jane,C,OFF,Dj
1235,Thomas,A,ON,Pr
1240,Thomas,I,ON,Pr
1304,Thomas,I,OFF,Dj
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The idea is calculate time difference between same "Name" with same "Tp" and OFF Dj&amp;nbsp; -&amp;gt; ON Pr (first in time(older), not second in time(newer)).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attention&amp;nbsp;Thomas has two calculations. And for example Joyce&amp;nbsp;don't&amp;nbsp;have correspond ON Pr in the table, so not need to calculate difference. Or for example&amp;nbsp;Carol is only one observation, no need to calculate difference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can not use the names because could be&amp;nbsp;hundreds of them xD. That why i try to do two do while statements to find all names needed. I know that It is very difficult to be efficient to do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Want

107,Joyce,B,OFF,Dj,0&lt;BR /&gt;110,Joyce,B,OFF,Dj,0
202,Jane,C,OFF,Dj,0
304,Alice,A,ON,Pr,0
476,Alice,A,OFF,Dj,172
546,Barbara,D,OFF,Dj,0
628,Carol,F,OFF,Dj,0
756,Mary,G,ON,Pr,0
858,Mary,F,ON,Pr,0
929,Mary,G,OFF,Dj,173
949,Thomas,I,ON,Pr,0
989,Thomas,I,OFF,Dj,40
1002,Jane,C,ON,Pr,0
1006,Jane,C,ON,Pr,0
1010,Jane,Z,ON,Pr,0
1100,Jane,C,OFF,Dj,98
1235,Thomas,A,ON,Pr,0
1240,Thomas,I,ON,Pr,0
1304,Thomas,I,OFF,Dj,64&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If i understand how to code this work i will be happy and will improve my programming skills to a next level.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry my last posts but to study programming we have to do work step by step, simple to complicated to achieve our final goal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;Aleixo&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 16:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450737#M283742</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-03T16:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450916#M283743</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;The idea is calculate time difference between same "Name" with same "Tp" and OFF Dj&amp;nbsp; -&amp;gt; ON Pr (first in time(older), not second in time(newer)).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure from your example data how to tell time sequence or "newer" or "older" or which order of calculation is desired:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the previous example I showed how to match things such as "same "Name" with same "Tp"&amp;nbsp;", Join on set1.value=set2.value and set1.value2=set2.value2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A WHERE clause might be able to filter the results:&amp;nbsp;&amp;nbsp; where&amp;nbsp;set1.variable &amp;gt; set2.variable (if a single variable&amp;nbsp;contains the order of comparison information) if you have multiple comparisons then add them to the&amp;nbsp;where clause. but since you did&amp;nbsp; not actual use variables names in your requirement clearly enough I can't make a specific recommendation.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 23:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450916#M283743</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-03T23:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450952#M283744</link>
      <description>&lt;P&gt;See this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.dados(label='Dados');
  infile datalines dsd truncover;
  input Time:32. Name:$8. Tp:$1. Age:$3. Type:$2.;
datalines4;
107,Joyce,B,OFF,Dj
110,Joyce,B,OFF,Dj
202,Jane,C,OFF,Dj
304,Alice,A,ON,Pr
476,Alice,A,OFF,Dj
546,Barbara,D,OFF,Dj
628,Carol,F,OFF,Dj
756,Mary,G,ON,Pr
858,Mary,F,ON,Pr
929,Mary,G,OFF,Dj
949,Thomas,I,ON,Pr
989,Thomas,I,OFF,Dj
1002,Jane,C,ON,Pr
1006,Jane,C,ON,Pr
1010,Jane,Z,ON,Pr
1100,Jane,C,OFF,Dj
1235,Thomas,A,ON,Pr
1240,Thomas,I,ON,Pr
1304,Thomas,I,OFF,Dj
;;;;
run;

proc sort data=dados;
by name tp time;
run;

data want;
set dados;
by name tp;
retain inittime;
if first.tp
then do;
  inittime = .;
  diff = 0;
end;
if age = 'ON' and inittime = .
then do;
  inittime = time;
  diff = 0;
end;
else if age = 'OFF' and inittime ne .
then do;
  diff = time - inittime;
  inittime = .;
end;
else diff = 0;
drop inittime;
run;

proc sort data=want;
by time;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Time    Name       Tp    Age    Type    diff

 107    Joyce      B     OFF     Dj        0
 110    Joyce      B     OFF     Dj        0
 202    Jane       C     OFF     Dj        0
 304    Alice      A     ON      Pr        0
 476    Alice      A     OFF     Dj      172
 546    Barbara    D     OFF     Dj        0
 628    Carol      F     OFF     Dj        0
 756    Mary       G     ON      Pr        0
 858    Mary       F     ON      Pr        0
 929    Mary       G     OFF     Dj      173
 949    Thomas     I     ON      Pr        0
 989    Thomas     I     OFF     Dj       40
1002    Jane       C     ON      Pr        0
1006    Jane       C     ON      Pr        0
1010    Jane       Z     ON      Pr        0
1100    Jane       C     OFF     Dj       98
1235    Thomas     A     ON      Pr        0
1240    Thomas     I     ON      Pr        0
1304    Thomas     I     OFF     Dj       64
&lt;/PRE&gt;
&lt;P&gt;matches what you wanted.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 06:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450952#M283744</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-04T06:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450971#M283745</link>
      <description>&lt;P&gt;thank you ballardw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought also&amp;nbsp;about where statement but i had problems with repeated observations, like in Name repeated and like in ON Pr repeated with same name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450971#M283745</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-04T08:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450973#M283746</link>
      <description>&lt;P&gt;WoW, It's amazing what you have done. So simple what you do, i am &lt;SPAN class="short_text"&gt;&lt;SPAN&gt;delighted&lt;/SPAN&gt;&lt;/SPAN&gt;. Worked very well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will try to understand how it really works the code and the loop. Thank you so much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Aleixo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:44:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450973#M283746</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-04-04T08:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450975#M283747</link>
      <description>&lt;P&gt;I started out with the simple retain/first./if-condition-then model. Then I ran the code, looked for differences to the desired result, and then tweaked the conditions until I got the desired result. SAS being an interpreting language (you don't have to wait for compiles before every test), it lends itself very well to this approach.&lt;/P&gt;
&lt;P&gt;A major part of the solution is the correct sort order for the logic, and getting back to the original order. This can sometimes necessitate the creation of a variable that keeps the original _n_.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/450975#M283747</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-04T08:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Working in multiple loops in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/451095#M283748</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/191516"&gt;@Aleixo&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;WoW, It's amazing what you have done. So simple what you do, i am &lt;SPAN class="short_text"&gt;&lt;SPAN&gt;delighted&lt;/SPAN&gt;&lt;/SPAN&gt;. Worked very well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will try to understand how it really works the code and the loop. Thank you so much&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Aleixo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here with some comments:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set dados;
by name tp;
retain inittime;
if first.tp
then do; /* initialize at start of a name/tp group */
  inittime = .;
  diff = 0; /* will always be zero at start */
end;
if age = 'ON' and inittime = .
then do; /* this is the first occurence after a start of group or OFF event */
  inittime = time; /* this also prevents the detection of an immediately following ON event */
  diff = 0;
end;
else if age = 'OFF' and inittime ne .
then do; /* this detects the change to OFF, but only when there wasn't one already */
  diff = time - inittime;
  inittime = .; /* prevents setting of diff = 0 for an immediately following OFF event */
end;
else diff = 0; /* in all other cases */
drop inittime;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 13:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Working-in-multiple-loops-in-a-dataset/m-p/451095#M283748</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-04T13:32:26Z</dc:date>
    </item>
  </channel>
</rss>

