<?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: How to create variable based on nested for + if with proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/719321#M222721</link>
    <description>&lt;P&gt;Hi, thanks for your reply!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it's kinda doing what I want sans the filtering by "Answered". I tried adding that but I can't get it to work, can you tell me where am I mistaken here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE work.dataMaxDate AS SELECT agreement_number, Max(Date) as MostRecentDate FROM work.data1 GROUP BY agreement_number;
CREATE TABLE work.final AS
SELECT 
	a.*
	,CASE when b.MostRecentDate is not null then 1 else 0 end as New_Var
FROM 
	originaldata a
	LEFT JOIN work.dataMaxDate (WHERE = (outcome = "Answered")) b on a.agreement_number = b.agreement_number and b.MostRecentDate &amp;gt;= a.date and b.MostRecentDate &amp;lt; a.date+2
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Feb 2021 12:09:02 GMT</pubDate>
    <dc:creator>catkat96</dc:creator>
    <dc:date>2021-02-15T12:09:02Z</dc:date>
    <item>
      <title>How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718892#M222518</link>
      <description>&lt;P&gt;I'm trying to create a table from information on another table. So, on the first one, let's call it Data1, I have panel data (many observations/individuals for many days).&lt;/P&gt;&lt;P&gt;On the second one, which I called "DataOutcome", I am trying to get one row per observation, and summarize something on the other table: if the last date of an observation is later than some day, then a variable will take value 0, or 1 in any other case.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code so far, it just gets the id's from another table and creates the date that would define this new variable's outcome.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apologies if this is a very bad question, I'm quite new at SAS/SQL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a data snippet:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data OriginalData;
   infile datalines delimiter=',';
   input date $ agreement_number $ outcome;
   datalines;
05/11/2020,1000,Answered
06/11/2020,1001,Answered
08/11/2020,1002,Not Answered
08/11/2020,1003,Answered
;
data Data1;
   infile datalines delimiter=',';
   input agreement_number $ date;
   datalines;
1000, 06/11/2020
1000, 07/11/2020
1001, 06/11/2020
1001, 07/11/2020
1001, 08/11/2020
1001, 09/11/2020
1002, 08/11/2020
1003, 08/11/2020
1003, 08/11/2020
;&lt;/CODE&gt;&lt;/PRE&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;proc SQL;
create table DataOutcome as
select Agreement_Number,
	Date,&lt;BR /&gt;    Outcome,
	intnx("day",Date,2) as new_date format=ddmmyy11.

from work.OriginalData
where outcome = "Answered";&lt;BR /&gt;
/* for each Agreement_Number&lt;BR /&gt;       if the last date in Data1 is between Date and new_date&lt;BR /&gt;           New_var = 1
*/
Group by 1;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 14:31:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718892#M222518</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-02-12T14:31:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718893#M222519</link>
      <description>&lt;P&gt;Please do provide a data snippet using DATALINES. This seems like it would be done more efficiently in a DATA step, but I can't really tell. I don't see a join condition anywhere in your code, either.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 14:12:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718893#M222519</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-02-12T14:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718899#M222524</link>
      <description>&lt;P&gt;Included a datalines snippet on the original post.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not using joins because I don't really need to join anything (I believe), I'm creating a completely new table... but perhaps there is a use for JOIN here that I'm not aware of.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 14:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718899#M222524</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-02-12T14:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718904#M222526</link>
      <description>&lt;P&gt;if i understand your objective correctly, i believe this code should do the trick:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data OriginalData;
   infile datalines delimiter=',';
   input date :ddmmyy10. agreement_number $ outcome $ ;
   format date ddmmyy10.;
   datalines;
05/11/2020,1000,Answered
06/11/2020,1001,Answered
08/11/2020,1002,Not Answered
08/11/2020,1003,Answered
;

data Data1;
   infile datalines delimiter=',';
   input agreement_number $ date :ddmmyy10.;
   format date ddmmyy10.;
   datalines;
1000, 06/11/2020
1000, 07/11/2020
1001, 06/11/2020
1001, 07/11/2020
1001, 08/11/2020
1001, 09/11/2020
1002, 08/11/2020
1003, 08/11/2020
1003, 08/11/2020
;

PROC SQL;
CREATE TABLE work.dataMaxDate AS SELECT agreement_number, Max(Date) as MostRecentDate FROM work.data1 GROUP BY agreement_number;
CREATE TABLE work.final AS
SELECT 
	a.*
	,CASE when b.MostRecentDate is not null then 1 else 0 end as New_Var
FROM 
	originaldata a
	LEFT JOIN work.dataMaxDate b on a.agreement_number = b.agreement_number and b.MostRecentDate &amp;gt;= a.date and b.MostRecentDate &amp;lt; a.date+2
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;note that you want to look at the work.final table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;good luck&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 14:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718904#M222526</guid>
      <dc:creator>utrocketeng</dc:creator>
      <dc:date>2021-02-12T14:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718978#M222539</link>
      <description>&lt;P&gt;I still think we're missing something. This code doesn't capture exactly what you put in your "OriginalData", but I don't know if you intend to count duplicate dates as "Answered". Some clarification there might help. You could also use PROC TRANSPOSE, I think. Nonetheless, PROC SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table 	want as
		select		
					agreement_number,
					min(date) as min_date
						format = mmddyy10.,
					case
						when calculated min_date = max(date) then "Not Answered"
						else "Answered"
					end as outcome
		from		
					data1
		group by
					agreement_number
		having
					agreement_number = min(agreement_number);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I highly recommend that you format your code. It helps spot errors more easily, like your semicolon after your WHERE clause in your query. Some people probably think my formatting is a little overkill, but consistency is key. Edit: it looks as though the "OriginalData" and "Data1" don't have the same dates. I hope I'm not missing something obvious.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you providing some example data.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 19:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/718978#M222539</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-02-12T19:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/719321#M222721</link>
      <description>&lt;P&gt;Hi, thanks for your reply!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it's kinda doing what I want sans the filtering by "Answered". I tried adding that but I can't get it to work, can you tell me where am I mistaken here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE work.dataMaxDate AS SELECT agreement_number, Max(Date) as MostRecentDate FROM work.data1 GROUP BY agreement_number;
CREATE TABLE work.final AS
SELECT 
	a.*
	,CASE when b.MostRecentDate is not null then 1 else 0 end as New_Var
FROM 
	originaldata a
	LEFT JOIN work.dataMaxDate (WHERE = (outcome = "Answered")) b on a.agreement_number = b.agreement_number and b.MostRecentDate &amp;gt;= a.date and b.MostRecentDate &amp;lt; a.date+2
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 12:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/719321#M222721</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-02-15T12:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create variable based on nested for + if with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/719359#M222738</link>
      <description>&lt;P&gt;Hi, thanks for the reply!&lt;/P&gt;&lt;P&gt;I think I might have not explained my problem properly. I have both OriginalData and Data1 already, I want to create a new table mixing info from both: I want to&lt;/P&gt;&lt;P&gt;-check OriginalData to identify those agreements that are "Answered"&lt;/P&gt;&lt;P&gt;-identify the date when that happened&lt;/P&gt;&lt;P&gt;-identify what is two days after that date (date + 2)&lt;/P&gt;&lt;P&gt;-check on the Data1 if those agreements' last date is larger than date + 2 (then assign value 0) or is between date and date + 2 (then assign value 1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there anything I'm forgetting to make it clear?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 15:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-variable-based-on-nested-for-if-with-proc-sql/m-p/719359#M222738</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-02-15T15:23:35Z</dc:date>
    </item>
  </channel>
</rss>

