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 ;
BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 2585 views
  • 2 likes
  • 6 in conversation