BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jrdykstr93
Obsidian | Level 7

Hello,

 

Right now, I have a data-set that looks like something along the following(lets call this data-set A):

 

 

var1var2
x2
x5
a7
a2
x7
b8

 

 

What i am trying to do, is loop through var2 and export different datasets based on the value of var2. So in this example, i would create four data-sets, each out corresponding to the unique amount of values in var2(var2 is numeric).  Right now, i grab the unique amount of values from a different data-set(lets call it B) and put them into a list with this code: 

proc sql; select var2 into :list separated by ' ' from B; quit;

From there I am trying to run the following code to create the data-sets.

%macro splitting;
%do i=1 %to %sysfunc(countw(&list.));
	%let block&i. = %scan(&list., &i.,%str());
	%put block&i.
		proc sql;
		    create table table&i. as 
		    select *
		    from A
		    where var2 contains &block&i.;
		quit;
%end;
%mend splitting; 

I could, go through and do many different if-then statements, but i would rather not considering how many different data-sets I need to create.  Can someone shed some light on the situation. Thanks.

1 ACCEPTED SOLUTION
4 REPLIES 4
Reeza
Super User
The general consensus is to not do this, by group processing is efficient though there are a few procs that don’t support it. In those cases, it’s still usually easier to filter on the fly with a WHERE option.

Most people still disregard this and use an approach as outlined in the posts from novinosrin.
jrdykstr93
Obsidian | Level 7

ok, sounds good. I guess why I am trying to do this is because I am trying to stack multiple occurrences of the same variable in the same by-group that i need to make the "ID" when i transpose, and then append the data-sets together   I will probably post another question based on this, than rather looping through macros.  Please take a look. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 978 views
  • 0 likes
  • 3 in conversation