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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 174 views
  • 0 likes
  • 3 in conversation