Help using Base SAS procedures

have AND in an IF/Else statement

Reply
Super Contributor
Posts: 399

have AND in an IF/Else statement

How could I change this to work wiht the and?

if V > 1then C ='Door' and D='Window";

else if V < 1 then C='Door2' and D2 = 'Window2".

Super Contributor
Posts: 1,636

Re: have AND in an IF/Else statement

if v>1 then do;

     c='Door';

     d='window';

   end;

else do;

     c='Door2';

     d='window2';

  end;

Super Contributor
Posts: 399

have AND in an IF/Else statement

What if I have something like this :

if V > 1then C ='Door' and D='Window";

else if V > 2 then C='Door2' and D2 = 'Window2"

else if V > 3 then C='Door3' and D= 'Window3':

Super User
Super User
Posts: 6,708

have AND in an IF/Else statement

AND is usually restricted to the logical operator in programming languages.

You are using AND in the sense of THIS AND ALSO THE OTHER.  In programming languages you usually use some type of grouping syntax to say that two or more things should go together.  In SAS you would use DO; ... END; statements to do this grouping.  (Also match sure to match your quotes properly)

if V > 1 then do; C ='Door' ;  D='Window'; end;

else if V > 2 then do; C='Door2'; D2 = 'Window2'; end;

else if V > 3 then do; C='Door3';  D= 'Window3'; end;

Super Contributor
Posts: 1,636

Re: have AND in an IF/Else statement

Hi Tom,

I think your code should be:

data have;

input v @@;

cards;

1 2 3 4 5 6

;

data want;

  set have;

if V > 3 then do; C='Door3';  D= 'Window3'; end;

else if V > 2 then do; C='Door2'; D = 'Window2'; end;

else if V > 1 then do; C ='Door' ;  D='Window'; end;

run;

proc print;run;

                           Obs    v      C         D

                             1     1

                            2     2    Door     Window

                            3     3    Door2    Window2

                            4     4    Door3    Window3

                            5     5    Door3    Window3

                            6     6    Door3    Window3

Thank you!

Linlin

Super User
Super User
Posts: 6,708

have AND in an IF/Else statement

Thanks. I just copied the code from above and put in DO; END; statements.

Ask a Question
Discussion stats
  • 5 replies
  • 172 views
  • 0 likes
  • 3 in conversation