<?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 shorten the conditional code with one similar condition and continuous numbers? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747573#M234659</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully by now you've seen my response to your other topic.&amp;nbsp; My response on the other topic was written to cover this topic as well:&amp;nbsp; I made the program data driven such that it really doesn't matter whether you have 7 countries or 7000 to deal with.&amp;nbsp; You just build a table listing everything out, and the program will use macros to generate the code for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you look closely at the code, you'll see that I'm doing all the setting of the variables in just three IF statements.&amp;nbsp; I don't know how much more I can streamline your IF statements than that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
    <pubDate>Sun, 13 Jun 2021 00:09:34 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2021-06-13T00:09:34Z</dc:date>
    <item>
      <title>How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747571#M234657</link>
      <description>&lt;P&gt;Hi all SAS experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a code as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if  geogn="UNITEDS"        &amp;amp; yr=1989 then cvt_yr=-4;
   else 
   if  geogn="UNITEDS"        &amp;amp; yr=1990 then cvt_yr=-3;
   else 
   if  geogn="UNITEDS"        &amp;amp; yr=1991 then cvt_yr=-2;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1992 then cvt_yr=-1;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1993 then cvt_yr=0;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1994 then cvt_yr=1;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1995 then cvt_yr=2;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1996 then cvt_yr=3;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1997 then cvt_yr=4;
      else 
   if  geogn="UNITEDS"        &amp;amp; yr=1998 then cvt_yr=5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am wondering how to shorten the code (because I need to do the same thing for 64 countries).&lt;STRONG&gt; And the time series is from 1990 to 2020, but I just want to focus on 9 years around 1993 as above (I mean, a condition for&amp;nbsp; -4 &amp;lt;= cvt_yr &amp;lt;= 5)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;Have a good weekend,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 06:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747571#M234657</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-06-13T06:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747573#M234659</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully by now you've seen my response to your other topic.&amp;nbsp; My response on the other topic was written to cover this topic as well:&amp;nbsp; I made the program data driven such that it really doesn't matter whether you have 7 countries or 7000 to deal with.&amp;nbsp; You just build a table listing everything out, and the program will use macros to generate the code for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you look closely at the code, you'll see that I'm doing all the setting of the variables in just three IF statements.&amp;nbsp; I don't know how much more I can streamline your IF statements than that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 00:09:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747573#M234659</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-13T00:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747574#M234660</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see your post already, thank you so much, I am trying to digest it. I will come back and hopefully you can help me to follow up my upcoming questions about the given code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and cheers,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 00:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747574#M234660</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-06-13T00:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747590#M234671</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for your dedicated help, however, now I want to focus only on one country, and as the edited question above, could you please give me a hint to deal with? Because in your previous code, you did not restrict the value of &lt;STRONG&gt;cvt_yr&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 06:56:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747590#M234671</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-06-13T06:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747601#M234679</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for your dedicated help, however, &lt;STRONG&gt;now I want to focus only on one country,&lt;/STRONG&gt; and as the edited question above, could you please give me a hint to deal with? Because in your previous code, you did not restrict the value of &lt;STRONG&gt;cvt_yr&lt;/STRONG&gt;.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You may need to define what you mean by focus on one country.&lt;/P&gt;
&lt;P&gt;Easiest might be to subset the data with a where:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   where    geogn="UNITEDS" and year in (1989:1998);
   cvt_yr= year-1993;
run;&lt;/PRE&gt;
&lt;P&gt;Or:&lt;/P&gt;
&lt;P&gt;one IF&lt;/P&gt;
&lt;PRE&gt; if geogn="UNITEDS" and year in (1989:1998) then cvt_yr= year-1993;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 10:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747601#M234679</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-13T10:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747612#M234687</link>
      <description>Make a format :&lt;BR /&gt;&lt;BR /&gt;proc format&lt;BR /&gt;invalue UNITEDS&lt;BR /&gt;1989=-4&lt;BR /&gt;1990=-3&lt;BR /&gt;.....&lt;BR /&gt;；&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;..........................&lt;BR /&gt;if  geogn="UNITEDS"  then cvt_yr=input(yr, UNITEDS. );</description>
      <pubDate>Sun, 13 Jun 2021 12:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747612#M234687</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-13T12:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to shorten the conditional code with one similar condition and continuous numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747620#M234693</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;STRONG&gt;the time series is from 1990 to 2020, but I just want to focus on 9 years around 1993 as above (I mean, a condition for&amp;nbsp; -4 &amp;lt;= cvt_yr &amp;lt;= 5)&lt;/STRONG&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;I guess I didn't catch that you were focusing on a particular range for cvt_yr.&amp;nbsp; What&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;have advised is good.&amp;nbsp; You could also use the very range that you coded above (-4 &amp;lt;= cvt_yr &amp;lt;= 5), like so:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET	GeoGn					=	UNITEDS;
%LET	StartYr					=	1993;

DATA	gen_post_treat;
	set matching;
	yr=input(year, ?? 32.);	/*year is a character variable, so I convert to numeric variable*/

	IF	STRIP(UPCASE(GeoGn))	=	"&amp;amp;GeoGn";

	Cvt_Yr						=	Yr	-	&amp;amp;StartYr;

	IF	-4 		&amp;lt;= 	cvt_yr 		&amp;lt;= 	5;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to switch to another year and start date, then you would edit the %LET statements.&amp;nbsp; Note that because these are macro statements that you do NOT put quotes around the GeoGn value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I code up some test data as follows:&lt;/P&gt;
&lt;PRE&gt;DATA	Matching;
	LENGTH	GeoGn	$12;
	LENGTH	Year	$4;
	INPUT	GeoGn	$
			Year	$
			;
DATALINES;
UNITEDS  1987
UNITEDS  1988
UNITEDS  1989
UNITEDS  1990
UNITEDS  1991
UNITEDS  1992
UNITEDS  1993
UNITEDS  1994
UNITEDS  1995
UNITEDS  1996
UNITEDS  1997
UNITEDS  1998
UNITEDS  1999
UNITEDS  2000
UNITEDS  2001
UNITEDS  2002
UNITEDS  2003
UNITEDS  2004
UNITEDS  2005
UNITEDS  2006
UNITEDS  2007
UNITEDS  2008
UNITEDS  2009
;
RUN;&lt;/PRE&gt;
&lt;P&gt;The above yields:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1623595459473.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60312i02D301C67923760F/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1623595459473.png" alt="jimbarbour_0-1623595459473.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Note that 1989 is in the data in this scenario.&amp;nbsp; Previously you had mentioned that 1989 was not to be included.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jun 2021 14:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-shorten-the-conditional-code-with-one-similar-condition/m-p/747620#M234693</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-13T14:46:32Z</dc:date>
    </item>
  </channel>
</rss>

