BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS_Question
Quartz | Level 8
/*Have*/

data insimp2;
	set sashelp.cars(keep=make type origin cylinders horsepower);
run;

proc sql;
	select distinct make into: vak1-
	from work.insimp2;
quit;

%put &sqlobs.;
%put &vak1.;

%MACRO GDATA(nbs,vak);
	proc sql;
		create table work.insimp2_&nbs. as
			select * from work.insimp2
			group by cylinders
				having (Make="&vak.")
				order by origin, horsepower asc;
	quit;
%MEND GDATA; 

/* WANT */

/*
I Want for every make of the car to run the macro. So the macro dynamically creates datasets with 1 make of every car
Obviously I can do it like this: 

%GDATA(1,&vak1.);
%GDATA(2,&vak2.);
etc.
%GDATA(38,&vak38.);


But in this case the dataset must always have 38 obs to keep working. 
But I want this macro to run without knowing that there are 38 makes (like in this case).
So maybe there could be 20 or maybe 200 makes.. 
the macro should run without hardcoding the obs.
How do I do this?  

*/

Thanks in Advance guys! 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No need to move the data into macro variables to generate the macro calls.

 

Just use the data in a data step instead.

proc freq data=insimp2;
  tables make / noprint out=counts;
run;

data _null_;
  set counts;
  call execute(cats('%nrstr(%gdata)(',_n_,make,')'));
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

No need to move the data into macro variables to generate the macro calls.

 

Just use the data in a data step instead.

proc freq data=insimp2;
  tables make / noprint out=counts;
run;

data _null_;
  set counts;
  call execute(cats('%nrstr(%gdata)(',_n_,make,')'));
run;
SAS_Question
Quartz | Level 8
SWEET! I love your simple solutions @Tom! Great! Thanks al lot! 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 387 views
  • 0 likes
  • 2 in conversation