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.
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.
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.