BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hey all
I have a dataset with statistics on which I want to create
plot graph for each unique objectname
There are a lot of unique objectname's like more than 100 so I cannot
plot them all in the same graph
I managed to create a list of unique objectname values using proc sql select distinct to create a second dataset containing only unique values, but now I cant find a way to loop through these unique values and call the macro that will create the plot graph ..

I tried to do it using a data step, knowing that it would most probably doesnt work :

data _null_;
set work.uniqueObjects;
%createPlot(ObjectName);
run;

The dataset containing the Unique objectsnames has only one variable named ObjectName

Thanks for your always great help
Yan
4 REPLIES 4
deleted_user
Not applicable
Oh Gosh, you are so close.

So you have a (tested) macro that will take an object name and build a plot, and now you just want to call that for each record in a table... have I got that right?

You could use a Call function called Call Execute() but I became disenchanted with that when it had a memory leak problem on Windows in Version 8. I favour a solution I shall call the Kent solution because in a discussion between He and Me, he gratuitously suggested my alternative approach was better because it was conservative of memory and allowed debugging that throwing commands into memory did not. Here is some code, just a couple of lines to add to your program.


FileName CODEINCL Temp;

Data _NULL_;
Set WORK.UNIQUEOBJECTS;
/* File CODEINCL; */
Put '%Create( ' OBJECTNAME ');';
Run;

%Include CODEINCL;

FileName CODEINCL Clear;


In the data step, the file reference is commented out so that the first time you run it you get all the commands written to the LOG. This allows you to verify the command is well formed. You can also be conservative and run it first time with an ( Obs = 5) option on the Set table so that you don't write many repetitive lines in the log. Don't forget the command must be complete; a common oversight is that there need to be two semi colons around the end of the Put statement. One is inside to write the command to the file, one outside to complete the SAS Put command.

Then when you remove comments from the FILE line, the file is written to and will be submitted in the %include call. Using a Temp reference for the file means SAS will ask the OS to manage the file creation. This means you don't need to think about where you might put it. It also works on all operating systems, so you needn't think about paths, permissions, blocking, deleting old files or anything else when you write the code.

BIG TRAP: Do NOT N O T NOT use double quotes at the Put statement. SAS will attempt to resolve the macro reference, and you don't want that to happen until the command is properly formed in the command file. It's a common problem, and can cause instability in your SAS session with some macros, so I suggest you also save the code before you submit it.

Kind regards

David
deleted_user
Not applicable
Thank you so much, it works perfectly
But im surprised to have to use a workaround using a tempfile
just to do this simple looping through variable values

well it works thats what counts
deleted_user
Not applicable
You need to look at the context of the commands. The macro calls will present code to the SAS Supervisor, which will then attempt to compile and execute it.

The problem is that you don't want it to compile and execute until it has done everything you wanted it to do with your source data, that is: building the SAS commands to execute your macro for each value in the source table.

Kind regards

David
Olivier
Pyrite | Level 9
Maybe some silly solution :
did you try to insert a BY statement in your graphic procedure ?
That would loop without effort (except the one of sorting or indexing the dataset before plotting) throught all values of ObjectName !

Regards.
Olivier

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 684 views
  • 0 likes
  • 2 in conversation