DATA Step, Macro, Functions and more

struggeling with sqplot in a loop

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

struggeling with sqplot in a loop

I have been struggling with producing SGPLOT for every user in my dataset.

Let's say this is the data:

 

data test;
	input user $ event number;
	datalines;
	John 1 100
	John 2 150
	John 3 200
	Roy 1 200
	Roy 2 250
	Roy 3 300
	David 1 600
	David 2 150
	David 3 800
;
run;

The idea is to produce a pdf with a bar chart for every user.

I have found an example:

 

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;

data _null_;
  do i=1949 to 1955;
    call execute(cats('ods pdf file="c:\year_',put(i,4.),'.pdf";));
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;

 

But in my case there is no column which could be used to run the loop on. I can obviously add one, but the reference for the file name and plot data should be the name of the user, not the loop column.

It's probably not that difficult but I just don't see it. 


Accepted Solutions
Solution
‎02-10-2017 07:59 AM
Super User
Posts: 5,083

Re: struggeling with sqplot in a loop

[ Edited ]

I have to assume you can put the SGPLOT code into play, using CALL EXECUTE.  Here's how you get the loop.

 

data _null_;

do until (last.user);

   set test;

   by user;

end;

call execute('data subset; set test; where user="' );

call execute(user);

call execute('"; run;');

** More CALL EXECUTES to generate the SGPLOT using the data set SUBSET;

run;

 

The top portion of the DATA step generates a subset of the TEST data (named SUBSET) for a single user.  Then the next part that you add will generate SGPLOT and related statements for that user.

View solution in original post


All Replies
Super User
Posts: 10,500

Re: struggeling with sqplot in a loop

Do you want a plot for each user or a plot in a separate file?

 

It is common when using a Call Execute approach to have a control data set with the key values, such as your name variable.

Data _null_;

   set control;

   <call execute statements>

run;

 

then the value of the NAME can be pulled from the control data set.

 

Also if your list of names is small enough SAS allows using Character values in do loops.

do name='John', 'Roy','David';

  <use name as you used i in the year example>

end;

Solution
‎02-10-2017 07:59 AM
Super User
Posts: 5,083

Re: struggeling with sqplot in a loop

[ Edited ]

I have to assume you can put the SGPLOT code into play, using CALL EXECUTE.  Here's how you get the loop.

 

data _null_;

do until (last.user);

   set test;

   by user;

end;

call execute('data subset; set test; where user="' );

call execute(user);

call execute('"; run;');

** More CALL EXECUTES to generate the SGPLOT using the data set SUBSET;

run;

 

The top portion of the DATA step generates a subset of the TEST data (named SUBSET) for a single user.  Then the next part that you add will generate SGPLOT and related statements for that user.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 110 views
  • 0 likes
  • 3 in conversation