BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Durlov
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

 


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

4 REPLIES 4
mohamed_zaki
Barite | Level 11

Yes, seems you are on the right track.

 

Read more here

 

Conditionally Writing Observations to One or More SAS Data Sets

Steelers_In_DC
Barite | Level 11

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.

Reeza
Super User

 

 


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.  

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

 

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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