Help using Base SAS procedures

Need to loop through unique variable from data step.

Reply
Contributor
Posts: 73

Need to loop through unique variable from data step.

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.

Super User
Posts: 17,796

Re: Need to loop through unique variable from data step.

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

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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

Respected Advisor
Posts: 3,777

Re: Need to loop through unique variable from data step.

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

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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?

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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.

Attachment
Respected Advisor
Posts: 3,777

Re: Need to loop through unique variable from data step.

Super User
Posts: 17,796

Re: Need to loop through unique variable from data step.

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.

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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.

Super User
Posts: 17,796

Re: Need to loop through unique variable from data step.

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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

Thanks for the link!

Super User
Posts: 17,796

Re: Need to loop through unique variable from data step.

yes

Super User
Posts: 17,796

Re: Need to loop through unique variable from data step.

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;

Contributor
Posts: 73

Re: Need to loop through unique variable from data step.

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!

Regular Contributor
Posts: 195

Re: Need to loop through unique variable from data step.

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

Ask a Question
Discussion stats
  • 14 replies
  • 973 views
  • 3 likes
  • 4 in conversation