proc sort data=cars out=sortcars; by type; run; data tourdetails vendordetails; set sortcar; by type; Money=Wheelbase *Length; output tourdetails; if First.type then do; VendorMoney=0; end; VendorMoney + Money; if Last.type then output vendordetails; run; proc print data=tourdetails; title 'Detail Records: Dollars Spent on Individual Tours'; run; proc print data=vendordetails; title 'Vendor Totals: Dollars Spent and Bookings by Vendor'; run;
iam new to SAS, can someone please explain me the flow of the code??
i have attached the input data below.
what will be the output?
I agree with @novinosrin's explanation but, given your code, VendorMoney won't even be in tourdetails since you drop that variable in your code. Also, you create sortcars in your proc sort step, but then set sortcar in your datastep. Obviously, the output you posted was not from the code you posted.
Art, CEO, AnalystFinder.com
Hi, what doubt do you have?
please check the output file below, why there is a zero in the tourdetails tables first field whereas vendor details doesn't have it
I'm afraid I can't open attachments. Can you please paste as text
type wheelbase Length Money VendorMoney
Hybrid | 103 | 175 | 18025 | 0 |
Hybrid | 95 | 55 | 5225 | 18025 |
Hybrid | 106 | 175 | 18550 | 23250 |
SUV | 106 | 189 | 20034 | 41800 |
SUV | 110 | 180 | 19800 | 20034 |
SUV | 111 | 184 | 20424 | 39834 |
Vendor Totals: Dollars Spent and Bookings by Vendor
type wheelbase Length Money VendorMoney
Hybrid | 103 | 175 | 18025 | 18025 |
Hybrid | 95 | 55 | 5225 | 23250 |
Hybrid | 106 | 175 | 18550 | 41800 |
SUV | 106 | 189 | 20034 | 20034 |
SUV | 110 | 180 | 19800 | 39834 |
SUV | 111 | 184 | 20424 | 60258 |
I meant about the vendormoney column in both the tables!!!
OK,
Here you go-:
1. At compile time, SAS reads the descriptive portion of the input dataset(cars) and builds the PDV. So you have all your variable names from your input dataset and your variables that is created in the program as assignment statements and initializes with a value of missing. However in this case, since Vendormoney's assignment is on sum statement, the initial assignment is a zero instead of missing.
2. At execution time or while execution happens, take your first obs from cars SAS read the values and starts processing. However your explicit output (output tourdetails; ) writes the obs to the output datasettourdetails even before calculating Vendormoney with its existing value that is zero.
3. However the output vendordetails statement does not execute before the sum statement, therefore you do not have zero.
In essence statements are executed sequentially, unless you use goto, return, link et al statements. Hope that helps
Thank you!! That was very helpful:)
I agree with @novinosrin's explanation but, given your code, VendorMoney won't even be in tourdetails since you drop that variable in your code. Also, you create sortcars in your proc sort step, but then set sortcar in your datastep. Obviously, the output you posted was not from the code you posted.
Art, CEO, AnalystFinder.com
I am sorry I should have removed those commands and should have posted.
You really should change the correct solution to the response provided by @novinosrin. I was simply pointing out the inconsistency in your code.
Art, CEO, AnalystFinder.com
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.