- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I would have thought this to be elementary but I cannot find statements/code that will accomplish the following:
I need to loop through unique values from a variable assigned in DATA step.
DATA A.CARS;
SET B.CARS;
KEEP MANUFACTURER MODEL YEAR COLOR DOORS ENGINE;
If i want to perform multiple actions against unique values of say color or model, how would I loop through? I've looked at setting up an array but I don't see a way to limit it to creating unique values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Can you explain some more of what you're trying to do? Some example data would be great.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sure, and I'm not even sure I'm going about this correctly or explaining it correctly, but here is what I'm trying to avoid:
DATA A.CARS;
SET B.CARS;
KEEP MANUFACTURER MODEL YEAR COLOR DOORS ENGINE PRICE;
PROC PLOT DATA=A.CARS;
WHERE COLOR='BLUE';
TITLE 'BLUE CARS';
VBAR YEAR / RESPONSE=PRICE;
RUN;
PROC PLOT DATA=A.CARS;
WHERE COLOR='RED';
TITLE 'RED CARS';
VBAR YEAR / RESPONSE=PRICE;
RUN;
PROC PLOT DATA=A.CARS;
WHERE COLOR='BLACK';
TITLE 'BLACK CARS';
VBAR YEAR / RESPONSE=PRICE;
RUN;
I want to be able to loop through every unique COLOR of car, without knowing all the colors in the data, and be able to perform one PROC PLOT statement. I'm trying to not have to hardcode variable values in the plots (in this simple example).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You need BY statement. Process "whatever" for every unique level of the grouping defined in the BY statement. You may also need PROC SORT.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry data_null_;
After I read what I wrote, I needed to clarify. Say I want to do multiple plots of the same unique variable.
PROC PLOT DATA=A.CARS;
WHERE COLOR='BLUE';
TITLE 'BLUE CARS';
VBAR YEAR / RESPONSE=PRICE;
RUN;
PROC TABULATE DATA=A.CARS;
WHERE COLOR='BLUE';
CLASS YEAR;
VAR PRICE;
TABLE PRICE / HOUR;
RUN;
Or did you mean applying the BY statement on the DATA step?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I guess it's better to show you my code (and no, I'm not proud, I'm just learning SAS) to show you where I am at.
You can see that I summarize the data USER.RMFINTRV by SYSTEM and HOUR variables. I then want to loop through each unique SYSTEM and plot a chart and draw a table. I don't want to repeat the PROC SGPLOT and PROC TABULATE with each hardcoded unique value in the SYSTEM variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think these links will help you get started.
36505 - Organizing output based on BY variable values: PROC DOCUMENT or Macro
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What does the tabulate proc have to do with multiple plots?
Are you trying to run a bunch of different procedures for one unique level and then the loop it?
If so you're looking at a macro.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I am trying to do 2+ (probably adding more in the future) procedures for each unique value of a variable before the next itteration.
Guess I need to pick up a macro book. Never used them yet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The real question is, can I imbed macro calls inside ODS statements, especially graphing procedures?
Thanks for the link!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
yes
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, you need a by statement (and the corresponding sort).
You may also want to look into the #byval and #byvar variables for your titles.
PROC PLOT DATA=A.CARS;
BY Color;
VBAR YEAR / RESPONSE=PRICE;
RUN;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Reeza, I have been using those in my original code. The code I'm uploading is just stuff I'm playing around with. But I do appreciate any helpful tips I can get!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Try the following SAS Code as per your requirement...Wish it matches with what you want...
You wrote, you want to perform multiple action for each unique value of particular variable say color...
%macro loop;
proc sql;
select distinct color into color_loop separated by ' '
from b.cards;
select count (distinct color) as tot_distinct_color
from b.cards;
quit;
%do i = 1 %to &tot_distinct_color.;
PROC PLOT DATA=B.CARS;
WHERE COLOR="%scan(&color_loop.,&i.)";
TITLE ="%scan(&color_loop.,&i.). CARS";
VBAR YEAR / RESPONSE=PRICE;
RUN;
PROC TABULATE DATA=B.CARS;
WHERE COLOR="%scan(&color_loop.,&i.)";
CLASS YEAR;
VAR PRICE;
TABLE PRICE / HOUR;
RUN;
%end;
%mend loop;
%loop;
Hope it works...
Thanks,
Urvish