SAS problem

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

SAS problem

Can anyone explain to me why the first version of the code generates 7 observations, while the 2nd version only generates 3? How does if-then statements work in SAS when the 'then statement' is taken away?

Thank you!

data test;

    infile '...\test.txt' dsd;

    input style $ @;

   if style='CONDO' or style='RANCH' then

    input sqfeet bedrooms baths street $ price: dollar10.;

run;

vs.

data test;

    infile '...\test.txt' dsd;

    input style $ @;

    if style='CONDO' or style='RANCH';

    input sqfeet bedrooms baths street $ price: dollar10.;

run;

Attachment

Accepted Solutions
Solution
‎10-17-2012 01:58 PM
Respected Advisor
Posts: 3,156

Re: SAS problem

Posted in reply to Davidliu494

Hi,

The 'if then' in the first datastep will only tell SAS what to read in when 'if' conditions are met; if not being met, the 'then' will not be executed, but the process will proceed to the bottome and output a record. The 'if' in the second datastep will do more that: It only passes records that meet the conditions to where after the 'if' statement stands, and process all of the following statement , including the implicit output at the bottom of datastep, so if conditions are not met, they will be deleted.

Haikuo

View solution in original post


All Replies
Solution
‎10-17-2012 01:58 PM
Respected Advisor
Posts: 3,156

Re: SAS problem

Posted in reply to Davidliu494

Hi,

The 'if then' in the first datastep will only tell SAS what to read in when 'if' conditions are met; if not being met, the 'then' will not be executed, but the process will proceed to the bottome and output a record. The 'if' in the second datastep will do more that: It only passes records that meet the conditions to where after the 'if' statement stands, and process all of the following statement , including the implicit output at the bottom of datastep, so if conditions are not met, they will be deleted.

Haikuo

Super User
Super User
Posts: 7,078

Re: SAS problem

Posted in reply to Davidliu494

Because you used a subsetting IF statement. So that dataset is subset in the way that you requested.

IF X;

is the same as

IF NOT (X) THEN DELETE;

Super Contributor
Posts: 1,041

Re: SAS problem

Hi,

Why are there two Input statements in the datastep???

data test;

    infile '...\test.txt' dsd;

    input style $ @;

   if style='CONDO' or style='RANCH' then

    input sqfeet bedrooms baths street $ price: dollar10.;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 238 views
  • 3 likes
  • 4 in conversation