<?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: Help with sas macro in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686854#M79571</link>
    <description>&lt;P&gt;Are your variables numeric or character.&amp;nbsp; You code seems confused about that.&lt;/P&gt;
&lt;P&gt;If they are characters that consist only of digits how are the value formatted?&amp;nbsp; Do they have leading zeros? Leading spaces?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You appear to be trying to convert variable the TERMID from&amp;nbsp;SID.SID_Termid from character to number (although why you are trying to use the name of the format BEST as an INFORMAT it is not clear).&amp;nbsp; And then you use the resulting macro variable as a character value to compare TERMID in&amp;nbsp; SID.SID_COURSES_TAKEN by adding quotes around it.&amp;nbsp; But you didn't tell SAS how to format the new number you made from the previous string.&amp;nbsp; So you will probably get an 8 or 12 character string with the digits right aligned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values are 6 digits with leading zeroes perhaps you want to use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select distinct quote(put(input(termid, 32.) -503,Z6.))
  into :Term_From separated by ' '
  from  SID.SID_Termid where Flagcurrentterm='Y'
;
...
where&amp;nbsp;TERMID&amp;nbsp;in&amp;nbsp;(&amp;amp;term_from)
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If they instead do not have leading zeros then perhaps you want to left align the value generated by PUT before adding the quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;quote(strip(put(input(termid, 32.) -503,6.)))&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Sep 2020 22:57:16 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-09-25T22:57:16Z</dc:date>
    <item>
      <title>Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686841#M79568</link>
      <description>&lt;P&gt;I try to create two macro variables to use throughout my programs. So the first gave me a value of 201505, and the second one gave me a list 15 terms up until 20201. These are what I want. However, when I execute the macro variables in later program. It only gave me one term information, which is the 201505 information. It might be because the TERMID is used twice in the macros and sas only recognizes the one used in first marco. What I really want to include in later program is the list of 15 terms generated by the second macro. Any ideas on how to modify the macros? Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*The first one calculates the term which is 5 years before*/&lt;BR /&gt;%GLOBAL  Term_From ;
PROC SQL; 	
SELECT (input(termid, best6.) -503)
	INTO	:Term_From
	FROM  SID.SID_Termid where Flagcurrentterm='Y';
quit;&lt;BR /&gt;&lt;BR /&gt;/* &amp;nbsp;The second one selects a list of 15&amp;nbsp; terms that could be included in later programs*/&lt;BR /&gt;%GLOBAL TERMCODE;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT TERMID &lt;BR /&gt;INTO :TERMCODE &lt;BR /&gt;FROM SID.SID_Termid WHERE TERMID IN (select a.termid from SID.SID_Termid a where a.latestbenchmark = 'E' AND a.termid ge Put((&amp;amp;Term_From), 6.));&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*&amp;nbsp;the&amp;nbsp;following&amp;nbsp;programs&amp;nbsp;will&amp;nbsp;execute&amp;nbsp;macro&amp;nbsp;variables*/&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	Create Table Crs_records AS
			SELECT count(*) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'
	        AND termid in ("&amp;amp;TERMCODE");

	Create table crs_rec_by_terms_bench AS
			SELECT  termid, bench, count(distinct UID) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'		
		    AND termid in ("&amp;amp;TERMCODE")
			GROUP BY termid, bench
			ORDER BY termid Desc, bench;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 21:45:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686841#M79568</guid>
      <dc:creator>xliu1</dc:creator>
      <dc:date>2020-09-25T21:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686845#M79569</link>
      <description>&lt;P&gt;The query for the 15 list isn't correct. Add &lt;STRONG&gt;separated by ","&lt;/STRONG&gt; to store them into a list, separated by a delimiter otherwise it only stores the last value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL; 	
SELECT (input(termid, best6.) -503)
	INTO	:Term_From separated by ", "
	FROM  SID.SID_Termid where Flagcurrentterm='Y';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could avoid the use of macro variables with a subquery though&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	Create table crs_rec_by_terms_bench AS
			SELECT  termid, bench, count(distinct UID) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'		
		    AND termid in (SELECT (input(termid, best6.) -503)
	FROM  SID.SID_Termid where Flagcurrentterm='Y')
			GROUP BY termid, bench
			ORDER BY termid Desc, bench;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/317497"&gt;@xliu1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I try to create two macro variables to use throughout my programs. So the first gave me a value of 201505, and the second one gave me a list 15 terms up until 20201. These are what I want. However, when I execute the macro variables in later program. It only gave me one term information, which is the 201505 information. It might be because the TERMID is used twice in the macros and sas only recognizes the one used in first marco. What I really want to include in later program is the list of 15 terms generated by the second macro. Any ideas on how to modify the macros? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*The first one calculates the term which is 5 years before*/&lt;BR /&gt;%GLOBAL  Term_From ;
PROC SQL; 	
SELECT (input(termid, best6.) -503)
	INTO	:Term_From
	FROM  SID.SID_Termid where Flagcurrentterm='Y';
quit;&lt;BR /&gt;&lt;BR /&gt;/* &amp;nbsp;The second one selects a list of 15&amp;nbsp; terms that could be included in later programs*/&lt;BR /&gt;%GLOBAL TERMCODE;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT TERMID &lt;BR /&gt;INTO :TERMCODE &lt;BR /&gt;FROM SID.SID_Termid WHERE TERMID IN (select a.termid from SID.SID_Termid a where a.latestbenchmark = 'E' AND a.termid ge Put((&amp;amp;Term_From), 6.));&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*&amp;nbsp;the&amp;nbsp;following&amp;nbsp;programs&amp;nbsp;will&amp;nbsp;execute&amp;nbsp;macro&amp;nbsp;variables*/&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	Create Table Crs_records AS
			SELECT count(*) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'
	        AND termid in ("&amp;amp;TERMCODE");

	Create table crs_rec_by_terms_bench AS
			SELECT  termid, bench, count(distinct UID) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'		
		    AND termid in ("&amp;amp;TERMCODE")
			GROUP BY termid, bench
			ORDER BY termid Desc, bench;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 21:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686845#M79569</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-25T21:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686849#M79570</link>
      <description>&lt;P&gt;Thanks for your prompt reply. I had tried subquery but it takes very long for sas to run because I need to pull over 9 million record. I followed&amp;nbsp; your solution and added&amp;nbsp;&lt;STRONG&gt;separated by ","&lt;/STRONG&gt; in the macro, and run the following programs, but I have no observations in the table. Any idea what is wrong? thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is sas log:&lt;/P&gt;&lt;P&gt;533 %GLOBAL TERMCODE;&lt;BR /&gt;534 PROC SQL;&lt;BR /&gt;535 SELECT TERMID&lt;BR /&gt;536 INTO :TERMCODE separated by ","&lt;BR /&gt;537 FROM SID.SID_Termid WHERE TERMID IN (select a.termid from SID.SID_Termid a where&lt;BR /&gt;537! a.latestbenchmark = 'E' AND a.termid ge Put((&amp;amp;Term_From), 6.));&lt;BR /&gt;538 QUIT;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 1.62 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;539 Proc sql;&lt;BR /&gt;540 Create Table Crs_records AS&lt;BR /&gt;541 SELECT count(*) AS Num_rec&lt;BR /&gt;542 FROM SID.SID_COURSES_TAKEN CRS&lt;BR /&gt;543 WHERE fee_class_kind ^= 'P'&lt;BR /&gt;544 AND termid in ("&amp;amp;TERMCODE");&lt;BR /&gt;NOTE: Table WORK.CRS_RECORDS created, with 1 rows and 1 columns.&lt;/P&gt;&lt;P&gt;545&lt;BR /&gt;546 Create table crs_rec_by_terms_bench AS&lt;BR /&gt;547 SELECT termid, bench, count(distinct UID) AS Num_rec&lt;BR /&gt;548 FROM SID.SID_COURSES_TAKEN CRS&lt;BR /&gt;549 WHERE fee_class_kind ^= 'P'&lt;BR /&gt;550 AND termid in ("&amp;amp;TERMCODE")&lt;BR /&gt;551 GROUP BY termid, bench&lt;BR /&gt;552 ORDER BY termid Desc, bench;&lt;BR /&gt;NOTE: Table WORK.CRS_REC_BY_TERMS_BENCH created, with 0 rows and 3 columns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 22:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686849#M79570</guid>
      <dc:creator>xliu1</dc:creator>
      <dc:date>2020-09-25T22:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686854#M79571</link>
      <description>&lt;P&gt;Are your variables numeric or character.&amp;nbsp; You code seems confused about that.&lt;/P&gt;
&lt;P&gt;If they are characters that consist only of digits how are the value formatted?&amp;nbsp; Do they have leading zeros? Leading spaces?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You appear to be trying to convert variable the TERMID from&amp;nbsp;SID.SID_Termid from character to number (although why you are trying to use the name of the format BEST as an INFORMAT it is not clear).&amp;nbsp; And then you use the resulting macro variable as a character value to compare TERMID in&amp;nbsp; SID.SID_COURSES_TAKEN by adding quotes around it.&amp;nbsp; But you didn't tell SAS how to format the new number you made from the previous string.&amp;nbsp; So you will probably get an 8 or 12 character string with the digits right aligned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values are 6 digits with leading zeroes perhaps you want to use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select distinct quote(put(input(termid, 32.) -503,Z6.))
  into :Term_From separated by ' '
  from  SID.SID_Termid where Flagcurrentterm='Y'
;
...
where&amp;nbsp;TERMID&amp;nbsp;in&amp;nbsp;(&amp;amp;term_from)
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If they instead do not have leading zeros then perhaps you want to left align the value generated by PUT before adding the quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;quote(strip(put(input(termid, 32.) -503,6.)))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Sep 2020 22:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686854#M79571</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-25T22:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686858#M79572</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;thanks so much! I tried your solution and it did work out in the programs. The column TERMID is a character variable. I use (input(termid, best6.) -503) to calculate the reference term that I want, which is 201505 (202008-503=201505). I put your quote statement in the second macro, and it gave me a list of terms with double quotes like "201505" ''201508"... then I execute the marco in later programs and I have 15 term information.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%GLOBAL TERMCODE;
PROC SQL;
SELECT quote(strip(put(input(termid, 32.),6.)))
INTO :TERMCODE separated by ", "
FROM SID.SID_Termid WHERE TERMID IN (select a.termid from SID.SID_Termid a where a.latestbenchmark = 'E' AND a.termid ge Put((&amp;amp;Term_From), 6.));
QUIT;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;this is how i execute the macro&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	Create Table Crs_records AS
			SELECT count(*) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'
	        AND termid in (&amp;amp;TERMCODE);

	Create table crs_rec_by_terms_bench AS
			SELECT  termid, bench, count(distinct UID) AS Num_rec
			FROM SID.SID_COURSES_TAKEN CRS  
			WHERE  fee_class_kind ^= 'P'		
		    AND termid in (&amp;amp;TERMCODE)
			GROUP BY termid, bench
			ORDER BY termid Desc, bench;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Sep 2020 23:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686858#M79572</guid>
      <dc:creator>xliu1</dc:creator>
      <dc:date>2020-09-25T23:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686942#M79576</link>
      <description>&lt;P&gt;That's a dangerous way to do that operation, I would highly recommend using appropriate SAS date functions instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt; I use (input(termid, best6.) -503) to calculate the reference term that I want, which is 201505 (202008-503=201505).&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2020 17:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686942#M79576</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-26T17:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686945#M79577</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;for your reply. I am new to SAS programming. Can you elaborate why it is dangerous to do that operation in sas? Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2020 18:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/686945#M79577</guid>
      <dc:creator>xliu1</dc:creator>
      <dc:date>2020-09-26T18:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/687086#M79579</link>
      <description>&lt;P&gt;Because it doesn't always work and if your data is inaccurate due to typos or some other issue it will still work but you won't realize that it's wrong. ie if your date is 202101and you subtract 503 you get 201598. What does that mean? Is that what you expect? Do you need this to work for more than a few months?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using date calculations that will property flip at the year end is less likely to run into issues over time.&lt;/P&gt;
&lt;P&gt;Look up the INTNX() and INTCK() formulas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a great, but longer and in depth, reference for dates and times in SAS&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/317497"&gt;@xliu1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;for your reply. I am new to SAS programming. Can you elaborate why it is dangerous to do that operation in sas? Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 01:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/687086#M79579</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-28T01:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help with sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/687177#M79580</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 12:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Help-with-sas-macro/m-p/687177#M79580</guid>
      <dc:creator>xliu1</dc:creator>
      <dc:date>2020-09-28T12:26:38Z</dc:date>
    </item>
  </channel>
</rss>

