<?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: Data manipulation - do loops in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569182#M11768</link>
    <description>Look at the RETAIN statement.</description>
    <pubDate>Wed, 26 Jun 2019 17:25:34 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-26T17:25:34Z</dc:date>
    <item>
      <title>Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569180#M11767</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm doing some data manipulation in SAS 9.4 before further analysis. I have several years of temperature data and want to do further calculations if there has been no killing frost in the fall yet. I wrote the following statements&amp;nbsp; to make frost =1 on days where the temperature was less than 2 degrees.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if temp&amp;lt;-2 and day&amp;gt;180 then frost=1;&lt;BR /&gt;else frost=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to make frost =1 for the rest of the days following the first frost in that year, and then reset for the next year?&lt;/P&gt;&lt;P&gt;I was exploring do loops but I'm not sure if that would fit what I'm looking for or not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 17:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569180#M11767</guid>
      <dc:creator>CalebN</dc:creator>
      <dc:date>2019-06-26T17:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569182#M11768</link>
      <description>Look at the RETAIN statement.</description>
      <pubDate>Wed, 26 Jun 2019 17:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569182#M11768</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-26T17:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569183#M11769</link>
      <description>&lt;P&gt;Maybe this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if temp&amp;lt;-2 and day&amp;gt;180 and frost=0 then frost+1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By the way, it helps if you show a portion of the data. This code fragment and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;'s suggestion work if there is one observation for each day (and so DO loops are not useful here), but it does not work for other layouts of the data (and maybe do loops work there); and we really shouldn't have to guess what your data looks like.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 17:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569183#M11769</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-26T17:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569189#M11770</link>
      <description>&lt;P&gt;RETAIN and BY group processing. Please supply example data (as advised when you posted).&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 17:33:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569189#M11770</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-26T17:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569202#M11771</link>
      <description>Thanks for your responses. I have attached a sample of my data to the original post.&amp;nbsp;I have one temperature observation per day and multiple years.</description>
      <pubDate>Wed, 26 Jun 2019 18:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569202#M11771</guid>
      <dc:creator>CalebN</dc:creator>
      <dc:date>2019-06-26T18:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569205#M11772</link>
      <description>I think that code would work for one year however, when the next year starts then frost would not equal zero. How can we make the code "reset" for the next year?</description>
      <pubDate>Wed, 26 Jun 2019 18:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569205#M11772</guid>
      <dc:creator>CalebN</dc:creator>
      <dc:date>2019-06-26T18:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569206#M11773</link>
      <description>Depends on the rest of the code. I can't download attachments.</description>
      <pubDate>Wed, 26 Jun 2019 18:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569206#M11773</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-26T18:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569215#M11774</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by year;
retain frost;
if first.year then frost = 0;
if day &amp;gt; 180 and temp &amp;lt;= 2 then frost = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jun 2019 18:09:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569215#M11774</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-26T18:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569253#M11776</link>
      <description>&lt;P&gt;A little complicated solution (than what has already been suggested) but this is "somewhat" an alternative solution. The data you have shared is for one year only and for first 100 days.&amp;nbsp;So not sure this will work out. Please let me know the outcome. I am curious how was the output of this code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Temp_Processed;
	SET Temp; /*Change the name for your dataset name*/
	RETAIN Year_Flag 0;
	RETAIN Frost 0;
	IF Year_Flag ^= 0 THEN OUTPUT;
	ELSE IF DAY &amp;gt; 180 and Temp &amp;lt; 2 and Year_Flag=0 THEN DO; Frost = 1; Year_Flag=Year; OUTPUT; END;
	ELSE IF DAY &amp;lt;= 180 THEN DO; Frost = 0; Year_Flag=0; OUTPUT; END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 19:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569253#M11776</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-06-26T19:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569265#M11783</link>
      <description>Thanks for your reply.&lt;BR /&gt;Not sure why the file is showing you only 100 days but the .csv I uploaded has a few years with 365 days for each year. When I ran your code, frost did = 1 on the day of the first frost in 1989 but then frost remained 1 for the rest of the days in following years. Not sure why.&lt;BR /&gt;Thanks</description>
      <pubDate>Wed, 26 Jun 2019 20:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569265#M11783</guid>
      <dc:creator>CalebN</dc:creator>
      <dc:date>2019-06-26T20:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569266#M11784</link>
      <description>&lt;P&gt;This code works for me! Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 20:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569266#M11784</guid>
      <dc:creator>CalebN</dc:creator>
      <dc:date>2019-06-26T20:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569315#M11790</link>
      <description>&lt;P&gt;I know why because I kept the first condition as not correct. I have changed it. I was tired last night and could not spot it (neither the error nor the fact that the excel file you sent had approximately 1460 rows. SAS Studio just outputs 100 rows at a time, by default. So I am sorry about all that. Here is the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Temp_Processed;
	SET Temp;/*Change as per your data name*/
	RETAIN Year_Flag 0;
	RETAIN Frost 0;

	IF Year_Flag ^=0 and DAY &amp;gt; 180 and Temp &amp;lt; 2 THEN
		OUTPUT;
	ELSE IF DAY &amp;gt; 180 and Temp &amp;lt; 2 and Year_Flag=0 THEN
		DO;
			Frost=1;
			Year_Flag=Year;
			OUTPUT;
		END;
	ELSE IF DAY &amp;lt;=180 THEN
		DO;
			Frost=0;
			Year_Flag=0;
			OUTPUT;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You can confirm that for&amp;nbsp; the intended Frost range, it is correctly assigned ... by doing a simple PROC SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	SELECT 
		MIN(DAY) AS StartingDate, 
		MAX(DAY) AS EndDate, 
		Year_Flag, 
		Frost 
	FROM 
		Temp_Processed 
	GROUP BY 
	Year_Flag, 
	Frost;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It outputs the intended result.&lt;/P&gt;&lt;P&gt;Sorry again for creating a confusion. Fortunately you got a solution that was working for you and thus your time was not lost by my solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2019 01:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569315#M11790</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-06-27T01:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Data manipulation - do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569323#M11791</link>
      <description>Elegant</description>
      <pubDate>Thu, 27 Jun 2019 04:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Data-manipulation-do-loops/m-p/569323#M11791</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-06-27T04:00:58Z</dc:date>
    </item>
  </channel>
</rss>

