DATA Step, Macro, Functions and more

Set, if and output statements

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Set, if and output statements

Hello, I just wanted to make sure I'm understanding this correctly (because it seems 30 mins ago, my understanding was incorrect). 

 

libname company 'SAS-data-library';
data hware inter soft;
set company.prices (keep = producttype price);
if price le 5.00;
if producttype = 'HARDWARE' then output HWARE;
else if producttype = 'NETWORK' then output INTER;
else if producttype = 'SOFTWARE' then output SOFT;
run;

So in the above piece of code, the if statements are linked to the set statement, right? As in an observation is only being inputted into hware from the company.prices dataset, if and only if it meets the criteria of producttype = "HARDWARE" and the price is less than $5. 

 

Just as a side not, previously I was thinking that the set and if statements were not linked and that the whole company.prices dataset was going into the hware, inter and soft.

 

Thanks!


Accepted Solutions
Solution
‎01-07-2016 02:31 PM
Super User
Posts: 17,750

Re: Set, if and output statements

[ Edited ]

 

 


the if statements are linked to the set statement, right? As in an observation is only being inputted into hware from the company.prices dataset, if and only if it meets the criteria of producttype = "HARDWARE" and the price is less than $5. 

I don't think that's the quite the correct understanding. The IF statement controls what is retained into the dataset, but all observations from the SET table are brought into the data step and then filtered and written to the requested data sets. You are correct in that not all records are automatically copied to the data subsets such as HWARE.

 

There's the data step and then the output data sets -> different ideas.  

 

An alternative is the WHERE statement that prevents records from being brought into the dataset in the first place.  

 

 

 

View solution in original post


All Replies
Super Contributor
Posts: 490

Re: Set, if and output statements

Yes, seems you are on the right track.

 

Read more here

 

Conditionally Writing Observations to One or More SAS Data Sets

Valued Guide
Posts: 858

Re: Set, if and output statements

It seems like you will get the desired outputs.  One thing to keep in mind is that an if statement reads in the entire dataset.  It would be advantageous to change 'if price le 5.00' to 'where price le 5.00'.  That way when you get to the if/then output statements you are reading a subset, not the entire dataset over again.  Along that same line of thought.  If you know that one output is going to be bigger than the rest you should start with that one and work incrementally towards the smallest output.

Solution
‎01-07-2016 02:31 PM
Super User
Posts: 17,750

Re: Set, if and output statements

[ Edited ]

 

 


the if statements are linked to the set statement, right? As in an observation is only being inputted into hware from the company.prices dataset, if and only if it meets the criteria of producttype = "HARDWARE" and the price is less than $5. 

I don't think that's the quite the correct understanding. The IF statement controls what is retained into the dataset, but all observations from the SET table are brought into the data step and then filtered and written to the requested data sets. You are correct in that not all records are automatically copied to the data subsets such as HWARE.

 

There's the data step and then the output data sets -> different ideas.  

 

An alternative is the WHERE statement that prevents records from being brought into the dataset in the first place.  

 

 

 

Super User
Super User
Posts: 7,392

Re: Set, if and output statements

You can of course update your code slighty, this datastep will read only 3 observations from the source rather than the full 5 records, so saves some reading time.  Also the other part of the where restricts to only those categories your interested in.  

data prices;
  length producttype $20;
  input producttype $ price;
datalines;
HARDWARE 3.23
HARDWARE 7.00
NETWORK 3.43
SOFTWARE 2.22
OTHER 10.00
;
run;

data hware inter soft;
  set prices (keep=producttype price where=(price le 5.00 and producttype in ("HARDWARE","NETWORK","SOFTWARE")));
  select(producttype);
    when("HARDWARE") output hware;
    when("NETWORK") output inter;
    otherwise output soft;
  end;
run;

 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 365 views
  • 2 likes
  • 5 in conversation