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


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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

9 REPLIES 9
novinosrin
Tourmaline | Level 20

Hi, what doubt do you have?

 

Hemanth003
Calcite | Level 5

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

novinosrin
Tourmaline | Level 20

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

Hemanth003
Calcite | Level 5

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!!!

novinosrin
Tourmaline | Level 20

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

Hemanth003
Calcite | Level 5

Thank you!! That was very helpful:)

art297
Opal | Level 21

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

 

 

Hemanth003
Calcite | Level 5

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

art297
Opal | Level 21

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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