data dsn dsn1;
set sashelp.class;
set sashelp.cars;
if class then output dsn ;
if cars then output dsn1;
proc print;
run;
data dsn;
set sashelp.class;
data dsn1;
set sashelp.cars;
run;
I want to use if condition
%macro split(dsn);
%if %upcase("&dsn") = "CLASS" %then %do;
data dsn;
set sashelp.&dsn;
%end;
%else %if %upcase("&dsn") = "CARS" %then %do;;
data dsn1;
set sashelp.&dsn;
%end;
%mend split;
%split(CLaSS)
%split(CArS)
This way, all variables from both datasets will end up in both output datasets. Do you really want this?
As @smantha and @Kurt_Bremser said, you can do it, but you probably won't like the results. Essentially it does a one-to-one merge (with no BY variable), and will stop reading data after the smaller data set is exhausted.
But it's worth playing around with, as understanding *why* you get these results will help you understand how the DATA step works.
data dsn dsn1;
set sashelp.class (in=a);
set sashelp.cars (in=b);
put (name sex make model a b)(=) ;
if a then output dsn ;
if b then output dsn1;
run ;
You might also explore how (and why) the results from two SET statements differ from the results with a single SET statement:
data dsn dsn1;
set sashelp.class (in=a) sashelp.cars (in=b);
put (name sex make model a b)(=) ;
if a then output dsn ;
if b then output dsn1;
run ;
It would be better to provide small example data sets and what you would expect for output from those examples.
Here's one very small example that uses two difference size data sets with different variables and combines them with the SET statements in the same manner.
data one; input x; datalines; 1 2 3 ; data two; input y; datalines; 99 88 77 66 ; data example; set one; set two; run;
The result matches the records in order but stops when the set with fewer records is exhausted. If you change the order of the SET statements you will get the same number of records with the same values but the order of the variables in the data set will change.
With this construct every record comes from both data sets. So the concept of "if from one set then output to a specific output data set" is pretty useless.
If you need that output of different records then both data sets would be on the same SET statement.
data fromone fromtwo; set one (in= in1) two (in= in2); ; if in1 then output fromone; if in2 then output fromtwo; run;
Which has the effect of adding the variables from the other data set with all missing values.
So, provide an example of what you have for two actual data sets and what you expect for the result.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: