BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1050 views
  • 1 like
  • 4 in conversation