BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Reader587
Calcite | Level 5

Hello all, I have this code and I want to create 3 different tables using a macro statement. Need some help. I can simply change &type1 to &type2 but what if I got 12 groups, what is an easier way to just do all 3? can I use a %do %to statement? 

 

proc sql number; create table complete_storm as
select Name,upcase(Basin) as Basin,maxwind,startdate,maxwind*1.60934 as maxwindkm,
startdate-enddate as stormlength,
case
when substr(Basin,2,1) eq "I" then "Indian"
when substr(Basin,2,1) eq "A" then "Atlantic"
else "Pacific"
end as Ocean
from forth.storm_summary_small
where Name is not missing;
quit;
proc sql;select distinct ocean /* Creating 3 macros */
into: type1- from complete_storm;quit;
%put &=type1;%put &=type2;%put &=type3;

proc sql;create table &type1 as
select * from complete_storm
where ocean eq "&type1";quit;

1 ACCEPTED SOLUTION

Accepted Solutions
3 REPLIES 3
Kurt_Bremser
Super User

Why do you need to split the data in the first place? You can always use

by ocean;

(after sorting) in procedure and data steps to do analysis per group.

Kurt_Bremser
Super User

You can use %DO if you create a variable with a list instead of a list of variables:

proc sql noprint;
select distinct ocean unto :types separated by " "
from complete_storm;
quit;

%macro process_list;
%local i type;
%do i = 1 %to %sysfunc(countw(&list.));
  %let type = %scan(&list.,&i.);
  /* process individual group stored in &type */
%end;
%mend;
%process_list /* call the macro */

but, as already stated, this is not necessary in 99% of cases.

PaigeMiller
Diamond | Level 26

There are no macros in your code. There are macro variables. Macros are not macro variables, the two should not be confused.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 538 views
  • 0 likes
  • 3 in conversation