Concatenating Datasets with conditions

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Concatenating Datasets with conditions

Is it possible to combine (concatenate) datasets using IF statement. If year is 2013, I want to concatenate the four datasets. If not then concatenate dataset one and two.

Data Combine;

Set

      If year(today() = 2013

then do;

      Dataset.one

      Dataset.two

      Dataset.three

      Dataset.four;

End;

Else do;

      Dataset.one

      Dataset.two;

End;

Run;


Accepted Solutions
Solution
‎06-20-2013 05:26 AM
Super Contributor
Posts: 276

Re: Concatenating Datasets with conditions

Posted in reply to sasthebest

Hi..

You almost done with your provided code,but just misplaced Set statement...

:-)

Try This..

Data Combine;

      If year(today()) = 2013

then do;

Set

      Dataset.one

      Dataset.two

      Dataset.three

      Dataset.four;

End;

Else Set

      Dataset.one

      Dataset.two;

Run;


Regards.

Sanjeev.K

View solution in original post


All Replies
Occasional Contributor
Posts: 12

Re: Concatenating Datasets with conditions

Posted in reply to sasthebest

Dude,

Try this code to solve your problem.

Data _null_;

call symput ('year', year(today()));

run;

%MACRO conc;

%IF &YEAR.=2013 %THEN %DO;

DATA COMBINE;

SET

      Dataset.one

      Dataset.two

      Dataset.three

      Dataset.four;

RUN;

%END;

%ELSE %DO;

Data Combine;

Set  Dataset.one

      Dataset.two;

RUN;

%END;

%MEND conc;

%conc;

Solution
‎06-20-2013 05:26 AM
Super Contributor
Posts: 276

Re: Concatenating Datasets with conditions

Posted in reply to sasthebest

Hi..

You almost done with your provided code,but just misplaced Set statement...

:-)

Try This..

Data Combine;

      If year(today()) = 2013

then do;

Set

      Dataset.one

      Dataset.two

      Dataset.three

      Dataset.four;

End;

Else Set

      Dataset.one

      Dataset.two;

Run;


Regards.

Sanjeev.K

Occasional Contributor
Posts: 12

Re: Concatenating Datasets with conditions

Posted in reply to kuridisanjeev

Hi Sanjeev,

I guess, This code will produce a syntax error.

Super Contributor
Posts: 276

Re: Concatenating Datasets with conditions

Don't think So..

are you getting any error msg while running my code ? or where do you think code produce the error???

Regards.

Sanjeev.K

Occasional Contributor
Posts: 18

Re: Concatenating Datasets with conditions

Posted in reply to kuridisanjeev

You make my day. However, I still have a question. How can I add this additional condition

if Country = Canada;

data tourrevenue;

    input Country $ 1-11 LandCost Vendor $ NumberOfBookings;

cards;

France       575 Express  10

Spain        510 World    12

Brazil       540 World     6

Canada       489 Express   .

Japan        720 Express  10

Greece       698 Express  20

Canada             1489 Southsea  6

Canada       425 World     8

Italy        468 Express   9

Russia       924 World     6

Switzerland  734 World    20

Australia   1079 Southsea 10

Ireland      558 Express   9

Canada       600 Goal      10

;

Run;

Data one two three four;

set tourrevenue;

if vendor = 'Express' then output one;

else if Vendor = 'World' then output two;

else if Vendor = 'Southsea' then output three;

else  output four ;

Run;

Data Combine;

      If year(today()) = 2013

then do;

Set  one

      two

      three

      four;

        if Country = Canada;

End;

Else do

        Set

      one

      two;

        if Country = Canada;

End;

Run;

Super User
Super User
Posts: 7,050

Re: Concatenating Datasets with conditions

Posted in reply to sasthebest

To do what you want you can just use a single WHERE clause.  All four datasets must have the variable COUNTRY.

data Combine;

  if year(today()) = 2013 then do;

     set one

         two

         three

         four

     ;

  end;

  else do;

     set one

         two

     ;

  end;

  where country = 'Canada';

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 296 views
  • 2 likes
  • 4 in conversation