BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to split one data set into multiple data sets.

Please find code for 3 ways to do it.

Why way3 is not working? The output data sets are empty.Why???

/*Way1*/
DATA out_Asia;
 set sashelp.cars(where=(origin='Asia'));
run;
DATA out_Europe;
 set sashelp.cars(where=(origin='Europe'));
run;
DATA out_USA;
 set sashelp.cars(where=(origin='USA'));
run;


/*Way2*/
data out_Asia   out_Europe  out_USA;
set sashelp.cars;
   if origin='Asia' then output out_Asia;
   else IF origin='Europe' then output out_Europe;
   else IF origin='USA' then output out_USA;
Run;


/*Way3*/
proc sql noprint;
 select distinct ORIGIN into :valList separated by '+' from SASHELP.CARS trimmed;
quit;
%put &valList;
/*Asia+Europe+USA*/

%let No_arguments=%sysfunc(countw(%str(&valList.),'+'));
%put &No_arguments.;


%macro mmacro1;
%do i=1 %to &No_arguments.;
%let tt=%scan(&valList.,&i.,+);
data out_&tt.; 
set sashelp.cars(where=(origin='tt')); 
run; 
%end;
%mend;
%mmacro1;
3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

You forgot to resolve the macrovariable in the where condition

 

where=(origin="&tt.")

Note that double quotes are necessary for the resolution to take place.

 

As often said on this forum, splitting a dataset is rarely necessary as most SAS procedures

allow by group processing.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

And what benefit will you get from such a process that offsets the downsides of: larger storage needs, slower processing, more complex less robust coding?  In almost all cases there is not benefit which outweighs those downsides.  Use SAS by group processing, its what it is for.

DavidBesaev
Fluorite | Level 6

Hello, @Ronein

 

I change that row and it has worked:

<span class="token keyword">set</span> sashelp<span class="token punctuation">.</span>cars<span class="token punctuation">(</span><span class="token statement">where</span><span class="token operator">=</span><span class="token punctuation">(</span>origin<span class="token operator">=</span><span class="token string">'tt'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

to: 

set sashelp.cars(where=(origin="&tt")); 
proc sql noprint;
 select distinct ORIGIN into :valList separated by '+' from SASHELP.CARS trimmed;
quit;
%put &valList;
/*Asia+Europe+USA*/

%let No_arguments=%sysfunc(countw(%str(&valList.),'+'));
%put &No_arguments.;


%macro mmacro1;
%do i=1 %to &No_arguments.;
%let tt=%scan(&valList.,&i.,+);
data out_&tt.; 
set sashelp.cars(where=(origin="&tt")); 
run; 
%end;
%mend;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 3 replies
  • 1328 views
  • 1 like
  • 4 in conversation