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. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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