<?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 Convert SAS Data step to SQL query in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860233#M339846</link>
    <description>&lt;P&gt;I have this SAS code using first. and recursive if else loops which I need to convert to an SQL query. But I am not able to understand how to do it because of the retain statement and calling numerous variables in multiple loops.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please help me to convert it to SQL query?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data new_data;
	set raw_data(where=(appr_date ne .));
	by loan_number period_dt;
	retain prev_appr_date prev_upd_appr_date appraisal_cross;

	if first.loan_number then do;
		if appr_date &amp;gt; period_dt then do;
			prev_appr_date=appr_date;
			upd_appr_date=period_dt;
			prev_upd_appr_date=upd_appr_date;
			appraisal_cross=1;
			x=1;
		end;
		else if appr_date &amp;lt;= period_dt then do; 
			prev_appr_date=appr_date;
			upd_appr_date=appr_date;
			prev_upd_appr_date=appr_date;
			appraisal_cross=0;
			x=2;
		end;
	end;

	else do;
		if appr_date&amp;lt;prev_upd_appr_date and appr_date ne prev_appr_date then do;
			upd_appr_date=max(period_dt,prev_upd_appr_date);
			appraisal_cross=1;
			x=3;
		end;
		else if appr_date=prev_appr_date and appraisal_cross=1 then do;
			upd_appr_date=prev_upd_appr_date;
			appraisal_cross=1;
			x=4;
		end;
		else if appr_date&amp;gt;=prev_upd_appr_date and appr_date&amp;gt;period_dt then do;
			upd_appr_date=period_dt;
			appraisal_cross=1;
			x=5;
		end;		
		else if appr_date&amp;gt;=prev_upd_appr_date and appr_date &amp;lt;=period_dt then do;
			upd_appr_date=appr_date;
			appraisal_cross=0;
			x=6;
		end;
	
		prev_upd_appr_date=upd_appr_date;
		prev_appr_date=appr_date;
	end;
run;&lt;/PRE&gt;&lt;P&gt;Thank you in advance. Hope to get some help on this.&lt;/P&gt;</description>
    <pubDate>Wed, 22 Feb 2023 17:33:59 GMT</pubDate>
    <dc:creator>rahulsaha2127</dc:creator>
    <dc:date>2023-02-22T17:33:59Z</dc:date>
    <item>
      <title>Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860233#M339846</link>
      <description>&lt;P&gt;I have this SAS code using first. and recursive if else loops which I need to convert to an SQL query. But I am not able to understand how to do it because of the retain statement and calling numerous variables in multiple loops.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please help me to convert it to SQL query?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data new_data;
	set raw_data(where=(appr_date ne .));
	by loan_number period_dt;
	retain prev_appr_date prev_upd_appr_date appraisal_cross;

	if first.loan_number then do;
		if appr_date &amp;gt; period_dt then do;
			prev_appr_date=appr_date;
			upd_appr_date=period_dt;
			prev_upd_appr_date=upd_appr_date;
			appraisal_cross=1;
			x=1;
		end;
		else if appr_date &amp;lt;= period_dt then do; 
			prev_appr_date=appr_date;
			upd_appr_date=appr_date;
			prev_upd_appr_date=appr_date;
			appraisal_cross=0;
			x=2;
		end;
	end;

	else do;
		if appr_date&amp;lt;prev_upd_appr_date and appr_date ne prev_appr_date then do;
			upd_appr_date=max(period_dt,prev_upd_appr_date);
			appraisal_cross=1;
			x=3;
		end;
		else if appr_date=prev_appr_date and appraisal_cross=1 then do;
			upd_appr_date=prev_upd_appr_date;
			appraisal_cross=1;
			x=4;
		end;
		else if appr_date&amp;gt;=prev_upd_appr_date and appr_date&amp;gt;period_dt then do;
			upd_appr_date=period_dt;
			appraisal_cross=1;
			x=5;
		end;		
		else if appr_date&amp;gt;=prev_upd_appr_date and appr_date &amp;lt;=period_dt then do;
			upd_appr_date=appr_date;
			appraisal_cross=0;
			x=6;
		end;
	
		prev_upd_appr_date=upd_appr_date;
		prev_appr_date=appr_date;
	end;
run;&lt;/PRE&gt;&lt;P&gt;Thank you in advance. Hope to get some help on this.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 17:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860233#M339846</guid>
      <dc:creator>rahulsaha2127</dc:creator>
      <dc:date>2023-02-22T17:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860238#M339847</link>
      <description>&lt;P&gt;Some things in a data step have no exact equivalent in PROC SQL. In particular, first.loan_number has no SQL equivalent, and there may be others things in your code that also don't translate.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 17:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860238#M339847</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-22T17:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860239#M339848</link>
      <description>&lt;P&gt;SQL is not the right tool to replicate this algorithm since it does not have the concept of FIRST or LAST.&amp;nbsp; It just operates on SETs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain the purpose of the code? Or is your actual question to understand what the code is doing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; There are no loops in that code, other than the implied loop over all of the observations being read from RAW_DATA.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 17:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860239#M339848</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-22T17:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860241#M339849</link>
      <description>I have to convert this code to Pyspark. Since I am unable to do that I was trying to convert it to sql, and then pass the sql query in Pyspark&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Feb 2023 18:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860241#M339849</guid>
      <dc:creator>rahulsaha2127</dc:creator>
      <dc:date>2023-02-22T18:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860242#M339850</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/401634"&gt;@rahulsaha2127&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have to convert this code to Pyspark. Since I am unable to do that I was trying to convert it to sql, and then pass the sql query in Pyspark&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So please explain in words what the code is doing. And perhaps better WHY it is doing that as part of the larger problem you are trying to solve.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 18:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860242#M339850</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-22T18:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SAS Data step to SQL query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860246#M339851</link>
      <description>&lt;P&gt;Use panda's instead or a method in pyspark that supports looping through each row.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or break the process down to it's steps and redesign it entirely using a SQL methodology which may involve multiple steps and reads.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 18:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SAS-Data-step-to-SQL-query/m-p/860246#M339851</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-22T18:22:53Z</dc:date>
    </item>
  </channel>
</rss>

