DATA Step, Macro, Functions and more

#Doubt regarding the flow of the code

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

#Doubt regarding the flow of the code

[ Edited ]


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?

 


Accepted Solutions
Solution
‎01-14-2018 06:49 PM
PROC Star
Posts: 8,103

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

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

 

 

View solution in original post


All Replies
PROC Star
Posts: 1,296

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

Hi, what doubt do you have?

 

Occasional Contributor
Posts: 6

Re: #Doubt regarding the flow of the code

Posted in reply to novinosrin

please check the output file below, why there is a zero in the tourdetails tables first field whereas vendor details doesn't have it

PROC Star
Posts: 1,296

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

I'm afraid I can't open attachments. Can you please paste as text

Occasional Contributor
Posts: 6

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

type wheelbase Length Money VendorMoney

Hybrid103175180250
Hybrid9555522518025
Hybrid1061751855023250
SUV1061892003441800
SUV1101801980020034
SUV1111842042439834

Vendor Totals: Dollars Spent and Bookings by Vendor

 type wheelbase Length Money VendorMoney

Hybrid1031751802518025
Hybrid9555522523250
Hybrid1061751855041800
SUV1061892003420034
SUV1101801980039834
SUV1111842042460258

 

 

I meant about the vendormoney column in both the tables!!!

PROC Star
Posts: 1,296

Re: #Doubt regarding the flow of the code

[ Edited ]
Posted in reply to Hemanth003

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

Occasional Contributor
Posts: 6

Re: #Doubt regarding the flow of the code

Posted in reply to novinosrin

Thank you!! That was very helpfulSmiley Happy

Solution
‎01-14-2018 06:49 PM
PROC Star
Posts: 8,103

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

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

 

 

Occasional Contributor
Posts: 6

Re: #Doubt regarding the flow of the code

I am sorry I should have removed those commands and should have posted.

PROC Star
Posts: 8,103

Re: #Doubt regarding the flow of the code

Posted in reply to Hemanth003

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

 

 

 

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 177 views
  • 0 likes
  • 3 in conversation