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!
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.
Yes, seems you are on the right track.
Read more here
Conditionally Writing Observations to One or More SAS Data Sets
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.
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.
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.