BookmarkSubscribeRSS Feed
G_I_Jeff
Obsidian | Level 7

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.

14 REPLIES 14
Reeza
Super User

Can you explain some more of what you're trying to do? Some example data would be great.

G_I_Jeff
Obsidian | Level 7

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).

data_null__
Jade | Level 19

You need BY statement.  Process "whatever" for every unique level of the grouping defined in the BY statement.  You may also need PROC SORT.

G_I_Jeff
Obsidian | Level 7

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?

G_I_Jeff
Obsidian | Level 7

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.

Reeza
Super User

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.

G_I_Jeff
Obsidian | Level 7

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.

G_I_Jeff
Obsidian | Level 7

The real question is, can I imbed macro calls inside ODS statements, especially graphing procedures?

Thanks for the link!

Reeza
Super User

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;

G_I_Jeff
Obsidian | Level 7

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!

UrvishShah
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 14 replies
  • 4344 views
  • 3 likes
  • 4 in conversation