BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data dsn dsn1;
set sashelp.class;
set sashelp.cars; 
if class then  output dsn	;
if cars  then  output dsn1;
proc print;
run;
7 REPLIES 7
VENKATAMAHESH
Calcite | Level 5
data dsn;
set sashelp.class;
data dsn1;
set sashelp.cars; 
run;
BrahmanandaRao
Lapis Lazuli | Level 10

I want to use if condition 

VENKATAMAHESH
Calcite | Level 5
%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)
smantha
Lapis Lazuli | Level 10
I hope you understand what you are doing when you have two set statements like that. SAS only processes the minimum number of rows from each of the datasets.
Second the output dataset will have variables from both datasets.
You need in= on the set statement if you want to check what dataset the record comes from.
Quentin
Super User

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 ;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ballardw
Super User

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 2981 views
  • 2 likes
  • 6 in conversation