<?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: Assigning value for a variable based on the order of year in each country in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717909#M222087</link>
    <description>&lt;P&gt;Relating to "&lt;SPAN&gt;Another question is to create a variable y equals 0 for every observation in the whole dataset." - use next statement in a data step:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain Y 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Feb 2021 14:25:11 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2021-02-09T14:25:11Z</dc:date>
    <item>
      <title>Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717873#M222072</link>
      <description>&lt;P&gt;Hi SAS Expert,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset named "treatment_sample" as attached. This dataset including 3 countries (Argentina, Australia, and Austria). A quick description as below:&lt;/P&gt;
&lt;PRE&gt;ENAME GEOGN     YEAR acc_pay ACC_PAY_TUR   ACC_STA         LAGS18&lt;BR /&gt;MOQ   AUSTRALIA 2002 2317    6.0917808219  Local standards 3.71&lt;BR /&gt;MOQ   AUSTRALIA 2004 781     13.70323168   Local standards 4.68&lt;BR /&gt;MOQ   AUSTRALIA 2005 1585    11.488588335  Local standards 4.58&lt;BR /&gt;MOQ   AUSTRALIA 2007 881     6.4921190893  IFRS            2.68&lt;BR /&gt;MOQ   AUSTRALIA 2008 893     4.3393461105  IFRS            1.78&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Year&lt;/STRONG&gt; is a character variable.&lt;/P&gt;
&lt;P&gt;Data of Australia is in years&amp;nbsp; 2001, 2002 and from 2004 to 2008&lt;/P&gt;
&lt;P&gt;Data of Austria&amp;nbsp;is in years&amp;nbsp; &amp;nbsp; 2004, 2005 and from 2007 to 2011&lt;/P&gt;
&lt;P&gt;Data of Belgium is in years&amp;nbsp; 2002, 2003 and from 2005 to 2009&lt;/P&gt;
&lt;P&gt;Data of Brazil is in years&amp;nbsp; &amp;nbsp; &amp;nbsp; 1998,1999 and from 2001 to 2005&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create two variables:&lt;/P&gt;
&lt;P&gt;Variable x=1 for every observation in the last five years of each country. For example, all observations in years from 2004 to 2008 in Australia will get the value 1. Or observations in years from 2007 to 2011 in Austria will get the value 1. Otherwise, x get value 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another question is to create a variable y equals 0 for every observation in the whole dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warm regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 12:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717873#M222072</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-09T12:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717879#M222075</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I want to create two variables:&lt;/P&gt;
&lt;P&gt;Variable x=1 for every observation in the last five years of each country. For example, all observations in years from 2004 to 2008 in Australia will get the value 1. Or observations in years from 2007 to 2011 in Austria will get the value 1. Otherwise, x get value 0.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have to determine the last five years from the data? Or can it be that for Australia we hard-code 2004 to 2008, and similarly hard-code the years for all the other countries? If we have to determine the last five years from the data, what should happen if the last five years in the data for a country are not consecutive?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717879#M222075</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-02-09T14:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717909#M222087</link>
      <description>&lt;P&gt;Relating to "&lt;SPAN&gt;Another question is to create a variable y equals 0 for every observation in the whole dataset." - use next statement in a data step:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain Y 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:25:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717909#M222087</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-09T14:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717913#M222091</link>
      <description>&lt;P&gt;You are not consistent with your description.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You wrote "&lt;SPAN&gt;This dataset including &lt;STRONG&gt;3 countries&lt;/STRONG&gt;" but you mention&amp;nbsp;&lt;STRONG&gt;4 countries&lt;/STRONG&gt; in the next lines.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;and please relate to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s post too.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717913#M222091</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-09T14:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717918#M222094</link>
      <description>&lt;P&gt;It's a little difficult to figure out what is going on. Instead of providing a data set, you should write a DATALINES statement that captures all your issues. People here, including myself, will not download or open an attachment. I've included my own data set based off your information, but I don't know if I captured all the idiosyncrasies of your data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ename :$3. geogn :$15. year $ acc_pay acc_pay_tur acc_sta :$15. lags18;
infile datalines dlm = "," dsd;
datalines;
MOQ,BELGIUM,2018,4122,6.0917808219,Local standards,2.33
MOQ,BELGIUM,2019,1231,6.0917808219,Local standards,14.22
MOQ,BELGIUM,2010,4122,6.0917808219,Local standards,1.21
MOQ,BELGIUM,2011,1231,6.0917808219,Local standards,10.71
MOQ,BELGIUM,2014,2317,6.0917808219,Local standards,3.71
MOQ,BELGIUM,2015,781,13.70323168,Local standards,4.68
MOQ,AUSTRALIA,1999,4122,6.0917808219,Local standards,1.21
MOQ,AUSTRALIA,2000,1231,6.0917808219,Local standards,10.71
MOQ,AUSTRALIA,2002,2317,6.0917808219,Local standards,3.71
MOQ,AUSTRALIA,2004,781,13.70323168,Local standards,4.68
MOQ,AUSTRALIA,2005,1585,11.488588335,Local standards,4.58
MOQ,AUSTRALIA,2007,881,6.4921190893,IFRS,2.68
MOQ,AUSTRALIA,2008,893,4.3393461105,IFRS,1.78
;
run;

proc sort data = have
	out = have_2;
		by geogn descending year; /* get the most recent year on top for each geogn */
run;

data have_3;
	set have_2;
	by geogn descending year; /* do the following for each geogn by year */
		y = 0; /* creating variable 'y' that is equal to 0 for the entire data set */
		if first.geogn then counter = 1; /* if it's the first country, then set counter = 1 */
			else counter + 1; /* Otherwise, if it's the same country, increment by 1 */
		if counter in (1:5) then x = 1; /* if the counter is in the first 5, create a flag called x and set to 1 */
			else x = 0; /* otherwise set it to 0. */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a helpful blog on by-group processing as well as first last processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html" target="_self"&gt;https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;'s method of using a retain statement is more efficient than what I included here.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/717918#M222094</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-02-09T14:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718061#M222145</link>
      <description>&lt;P&gt;Hi, does the below help using an example dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ename :$3. geogn :$15. year $ acc_pay acc_pay_tur acc_sta :$15. lags18;
	infile datalines dlm = "," dsd;
	num_year = input(year,best12.);
	datalines;
MOQ,BELGIUM,2018,4122,6.0917808219,Local standards,2.33
MOQ,BELGIUM,2019,1231,6.0917808219,Local standards,14.22
MOQ,BELGIUM,2010,4122,6.0917808219,Local standards,1.21
MOQ,BELGIUM,2011,1231,6.0917808219,Local standards,10.71
MOQ,BELGIUM,2014,2317,6.0917808219,Local standards,3.71
MOQ,BELGIUM,2015,781,13.70323168,Local standards,4.68
MOQ,AUSTRALIA,1999,4122,6.0917808219,Local standards,1.21
MOQ,AUSTRALIA,2000,1231,6.0917808219,Local standards,10.71
MOQ,AUSTRALIA,2002,2317,6.0917808219,Local standards,3.71
MOQ,AUSTRALIA,2004,781,13.70323168,Local standards,4.68
MOQ,AUSTRALIA,2005,1585,11.488588335,Local standards,4.58
MOQ,AUSTRALIA,2007,881,6.4921190893,IFRS,2.68
MOQ,AUSTRALIA,2008,893,4.3393461105,IFRS,1.78
;
run;

proc means data=have noprint nway;
	class geogn;
	var num_year;
	output out=mini_have (drop=_type_ _freq_) max=/autoname;
run;

proc sort data=have;
	by ename geogn;
run;

data have2 (drop=num_:);
	merge have mini_have;
	by geogn;
	retain y 0;

	/* X variable = 1 if it's within last 5 years of that particular Country*/
	if num_year_max - num_year &amp;lt; 5 then
		x=1;
	else x = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Feb 2021 23:03:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718061#M222145</guid>
      <dc:creator>qoit</dc:creator>
      <dc:date>2021-02-09T23:03:31Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718104#M222165</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your pointing, it is my bad for confusing you.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Do you have to determine the last five years from the data? Or can it be that for Australia we hard-code 2004 to 2008, and similarly hard-code the years for all the other countries? If we have to determine the last five years from the data, what should happen if the last five years in the data for a country are not consecutive?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The last five years here is as documented in my questions then, they are different depending on the countries.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Data of Australia is in years&amp;nbsp; 2001, 2002 and from 2004 to 2008&lt;/P&gt;
&lt;P&gt;Data of Austria&amp;nbsp;is in years&amp;nbsp; &amp;nbsp; 2004, 2005 and from 2007 to 2011&lt;/P&gt;
&lt;P&gt;Data of Belgium is in years&amp;nbsp; 2002, 2003 and from 2005 to 2009&lt;/P&gt;
&lt;P&gt;Data of Brazil is in years&amp;nbsp; &amp;nbsp; &amp;nbsp; 1998,1999 and from 2001 to 2005&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in this case, the &lt;STRONG&gt;last five years&lt;/STRONG&gt; of Austrialia is 2004 to 2008, of Austria is 2007 to 2011, Belgium is 2005 to 2009, and Brazil is 2001 to 2005.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if I understand you improperly.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Wed, 10 Feb 2021 03:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718104#M222165</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-10T03:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718105#M222166</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for such an inconsistency, it is 4 countries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 03:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718105#M222166</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-10T03:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718114#M222172</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/246836"&gt;@qoit&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;Many thanks for your answer, I am able to understand it.&lt;/P&gt;
&lt;P&gt;I just want to know if the code below is okay, I tested successfully but want to make sure my change does not cause any hidden problem that I did not notice enough&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname sample "C:\Users\pnguyen\Desktop\New folder";


data treatment;
	set sample.treatment_sample;
   num_year = input(year,best12.);
run;



proc means data=treatment noprint nway;
	class geogn; /*I just class only by geogn because I do not know why I need to sort by ename*/
	var num_year;
	output out=mini_have (drop=_type_ _freq_) max=/autoname;
run;

proc sort data=treatment;
	by geogn;
run;

data have2 (drop=num_:);
	merge treatment mini_have;
	by geogn;
	retain y 0;

	/* X variable = 1 if it's within last 5 years of that particular Country*/
	if num_year_max - num_year &amp;lt; 5 then
		x=1;
	else x = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks and warmest regards.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 04:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718114#M222172</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-10T04:28:22Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning value for a variable based on the order of year in each country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718115#M222173</link>
      <description>No worries, based on what you are trying to achieve, above should definitely work! Good luck &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 10 Feb 2021 04:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-value-for-a-variable-based-on-the-order-of-year-in/m-p/718115#M222173</guid>
      <dc:creator>qoit</dc:creator>
      <dc:date>2021-02-10T04:34:54Z</dc:date>
    </item>
  </channel>
</rss>

