<?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: Sgplot in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247442#M46405</link>
    <description>&lt;P&gt;But the code doesn't wor even if I get rid of ods pdf statement at all&lt;/P&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;options mprint mlogic;
%print;
%do i=1949 to 1955 by 1;
		proc sgplot data=test (where=(year=&amp;amp;i));
		vbar month/response=air;
		title &amp;amp;i;
	run;
	%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Feb 2016 13:04:33 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2016-02-02T13:04:33Z</dc:date>
    <item>
      <title>Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247437#M46401</link>
      <description>&lt;P&gt;It's a basic question but I sometimes get completely&amp;nbsp;lost with loops in SAS. I am trying to use the code below to get separate bar graphs for yeach year between 1949 and 1955, but I get an error. What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set sashelp.air;
	year=year(date);
	month=month(date);
run;

options mprint mlogic;
%print;
%do i=1949 to 1955 by 1;
	ods pdf file = 'c:\year_&amp;amp;i.pdf'
	proc sgplot data=test (where=(year=&amp;amp;i));
		vbar month/response=air;
		title &amp;amp;i;
	run;
	ods pdf close;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2016 12:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247437#M46401</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-02-02T12:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247441#M46404</link>
      <description>&lt;P&gt;You enclosed the filename in single quotes, suppressing resolution of the macro variable &amp;amp;i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: this was just the first problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the %do, you must also use %to and %by, instead of "to 1955 by 1". And the "%by 1" can be omitted, as this is the default.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro print;
%do i=1949 %to 1955;
  ods pdf file = "c:\year_&amp;amp;i.pdf";
  proc sgplot data=test (where=(year=&amp;amp;i));
    vbar month/response=air;
    title &amp;amp;i;
  run;
  ods pdf close;
%end;
%mend;
%print;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247441#M46404</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-02T13:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247442#M46405</link>
      <description>&lt;P&gt;But the code doesn't wor even if I get rid of ods pdf statement at all&lt;/P&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;options mprint mlogic;
%print;
%do i=1949 to 1955 by 1;
		proc sgplot data=test (where=(year=&amp;amp;i));
		vbar month/response=air;
		title &amp;amp;i;
	run;
	%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247442#M46405</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-02-02T13:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247443#M46406</link>
      <description>&lt;P&gt;Take another look at my code now, had to do some edits. I also couldn't test it, as I don't have SASHELP.AIR.&lt;/P&gt;
&lt;P&gt;If further problems persist, please post the log.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247443#M46406</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-02T13:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247445#M46407</link>
      <description>&lt;P&gt;Sorry,&amp;nbsp;I thought sashelp files are available to every sas user. Below is the code with a sample dataset and the log message&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	input year month air;
	datalines;
	1949 1 100
	1949 2 150
	1949 3 200
	1950 1 200
	1950 2 250
	1950 3 300
	1951 1 600
	1951 2 150
	1951 3 800
;
run;

options mprint mlogic;
%print;
%do i=1949 %to 1951;
	
	proc sgplot data=test (where=(year=&amp;amp;i));
		vbar month/response=air;
		title &amp;amp;i;
	run;
	
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the log is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;15 data test;&lt;/P&gt;
&lt;P&gt;16 input year month air;&lt;/P&gt;
&lt;P&gt;17 datalines;&lt;/P&gt;
&lt;P&gt;NOTE: The data set WORK.TEST has 9 observations and 3 variables.&lt;/P&gt;
&lt;P&gt;NOTE: DATA statement used (Total process time):&lt;/P&gt;
&lt;P&gt;real time 0.02 seconds&lt;/P&gt;
&lt;P&gt;cpu time 0.03 seconds&lt;/P&gt;
&lt;P&gt;　&lt;/P&gt;
&lt;P&gt;27 ;&lt;/P&gt;
&lt;P&gt;28 run;&lt;/P&gt;
&lt;P&gt;29&lt;/P&gt;
&lt;P&gt;30 options mprint mlogic;&lt;/P&gt;
&lt;P&gt;31 %print;&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;180&lt;/P&gt;
&lt;P&gt;WARNING: Apparent invocation of macro PRINT not resolved.&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;ERROR: The %DO statement is not valid in open code.&lt;/P&gt;
&lt;P&gt;32 %do i=1949 %to 1951;&lt;/P&gt;
&lt;P&gt;33&lt;/P&gt;
&lt;P&gt;34 proc sgplot data=test (where=(year=&amp;amp;i));&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;22&lt;/P&gt;
&lt;P&gt;76&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: nazwa, łańcuch w nawiasach,&lt;/P&gt;
&lt;P&gt;stała numeryczna, stała daty i godziny, brak danych, INPUT, PUT.&lt;/P&gt;
&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;79&lt;/P&gt;
&lt;P&gt;ERROR 79-322: Expecting a ).&lt;/P&gt;
&lt;P&gt;34 ! proc sgplot data=test (where=(year=&amp;amp;i));&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;22&lt;/P&gt;
&lt;P&gt;WARNING: Apparent symbolic reference I not resolved.&lt;/P&gt;
&lt;P&gt;ERROR: Błąd składniowy w warunku WHERE. &amp;lt;- it says "Syntax error in WHERE clause"&lt;/P&gt;
&lt;P&gt;34 proc sgplot data=test (where=(year=&amp;amp;i));&lt;/P&gt;
&lt;P&gt;-&lt;/P&gt;
&lt;P&gt;180&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Missing ')' parenthesis for data set option list&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;35 vbar month/response=air;&lt;/P&gt;
&lt;P&gt;36 title &amp;amp;i;&lt;/P&gt;
&lt;P&gt;WARNING: Apparent symbolic reference I not resolved.&lt;/P&gt;
&lt;P&gt;37 run;&lt;/P&gt;
&lt;P&gt;38&lt;/P&gt;
&lt;P&gt;39 %end;&lt;/P&gt;
&lt;P&gt;ERROR: The %END statement is not valid in open code.&lt;/P&gt;
&lt;P&gt;40 %mend;&lt;/P&gt;
&lt;P&gt;ERROR: No matching %MACRO statement for this %MEND statement.&lt;/P&gt;
&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;/P&gt;
&lt;P&gt;NOTE: PROCEDURE SGPLOT used (Total process time):&lt;/P&gt;
&lt;P&gt;real time 1:41.68&lt;/P&gt;
&lt;P&gt;cpu time 0.93 seconds&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247445#M46407</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-02-02T13:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247446#M46408</link>
      <description>&lt;P&gt;Why do you need 1 file per graph out of interest? &amp;nbsp;As for your code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  do i=1949 to 1955;
    call execute(cats('ods pdf file="c:\year_',put(i,4.),'.pdf";));&lt;BR /&gt;    call execute(cats('title "',put(i,4.),'";'));
    call execute(cats('proc sgplot data=test (where=(year=',put(i,4.),')); vbar month/response=air; run;'));
    call execute('ods pdf close;');
  end;
run;&lt;/PRE&gt;
&lt;P&gt;However there are two other methods to get what you want. &amp;nbsp;Firstly, and the one I would generally use, is to not output to a new file each time. &amp;nbsp;In most cases there is no need to, then you can just use by group processing, which is quicker, and simpler coding:&lt;/P&gt;
&lt;PRE&gt;ods pdf file="c:\all.pdf";
proc sgplot data=test;
  by year;
  title "#byvar1";
  vbar month/response=air;
run;
ods pdf close;&lt;/PRE&gt;
&lt;P&gt;Far simpler. &amp;nbsp;You could also do it in Graph Template Language, dynamic variables/conditional templates - which for this example might be a bit overkill, but it is well worth learning for more complicated graphs, good help is here:&amp;nbsp;&lt;A href="http://blogs.sas.com/content/graphicallyspeaking/" target="_blank"&gt;http://blogs.sas.com/content/graphicallyspeaking/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247446#M46408</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-02T13:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247448#M46409</link>
      <description>&lt;P&gt;Since each plot is a bar chart, I recommend that you use the SGPANEL procedure, which will create all charts in a single panel and automatically add the category to each plot:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sgpanel data=test;
  panelby year / columns=1;
  vbar month/response=air;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:34:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247448#M46409</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-02-02T13:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247450#M46411</link>
      <description>&lt;P&gt;The definition of a macro MUST start with a %macro statement, and then you need to invoke the macro by its name.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247450#M46411</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-02-02T13:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247454#M46413</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;Thanks for the code. I'll try it. I know GTL (at least I've heard of it, not that I have great experience in this area) but I agree that for this example might be a bit overkill - I would rather do it manually &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;I looked at your code and I see that you used the put function. So the problem is that SAS does not treat subsequent values of i as numbers but as text? So it tries to run the code like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sgplot data=test (where=(year="1949"))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;while year is numeric and this causes a problem. Am I right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;@RW9 @&lt;A class="lia-link-navigation lia-page-link lia-user-name-link" id="link_42" style="color: rgb(0, 125, 195);" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684" target="_self"&gt;&lt;SPAN&gt;Rick_SAS&lt;/SPAN&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know sgpanel but it is not suitable. I need a separate graph for each value of i. I just used the sashelp.air as the example. In real case it's not a graph for separate year but for separate client ID. I don't want them to be all mixed up in&amp;nbsp;one file.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:46:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247454#M46413</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-02-02T13:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sgplot in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247457#M46415</link>
      <description>&lt;P&gt;With regards to your question on put to text, yes this is correct. &amp;nbsp;Call execute() is a function which takes a string, and puts it directly into the compiler after the finish of the current datastep. &amp;nbsp;So what I do within the brackets is to create a string, the put just creates a string of the four numbers. &amp;nbsp;So the final string given to the call execute is:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;proc sgplot data=test (where=(year=1949))&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now when the compiler compiles this text, it still sees the number 1949 as it would from normal code.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 13:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sgplot-in-a-do-loop/m-p/247457#M46415</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-02T13:57:30Z</dc:date>
    </item>
  </channel>
</rss>

