DATA Step, Macro, Functions and more


Frequent Contributor
Posts: 76


Refer to the below sample transaction table, where sales are summarized at every transaction level mapped to a customer ID. Your task to write efficient SAS codes to answer the following questions: --------------------------------------------------------------------------------------a. Summarize sales & number of customers by visit number. For example, customer B made his 1st visit on 14 Jan & 2nd visit on 19 Mar. ------------------------------------------------------------------------------------------b. Summarize sales & number of customers by number of visits . For example, customer A has made 4 visits, Customer B has made 2 visits, etc Your answer for the above scenarios I have written the following code : data test; informat trans_date date9.; input cus_id $ trans_date trans_id sales; format trans_date date9.; cards; A 10-jan-16 102254 166 A 20-jan-16 109690 24 A 05-feb-16 103680 52 A 08-mar-16 116647 99 B 14-jan-16 110551 104 B 19-mar-16 115248 98 C 05-feb-16 111657 163 D 29-jan-16 112647 84 D 29-jan-16 116793 191 D 03-mar-16 116398 116 D 06-feb-16 112700 192 D 04-apr-16 117257 107 E 01-mar-16 118569 195 E 08-mar-16 109485 131 F 22-mar-16 112171 129 F 08-apr-16 113826 44 F 16-apr-16 114416 160 ; run; proc sort data=test; by cus_id; run; data test2(rename=x=visit_number); set test; by cus_id; if first.cus_id then x=1; else x+1; run; *** first scenario***** proc sql; create want1 as select sum(sales) as sales_sum,count(cus_id) as no_of_customers,visit_number from test2 group by visit_number; quit; ** second scenario*** proc sql; create table test3 as select sum(sales) as sale,cus_id,count(visit_number) as count_visit from test2 group by cus_id; quit; proc sql; select sale ,count(cus_id) as no_of_customers,count_visit as no_of_visits from test3 group by count_visit; quit; Could you please tell me whether that code is fine or correct me if anythng is wrong.

Super User
Posts: 10,574


Please use some formatting in your post.

For SAS code or logs, use the "little running man" and {i} buttons to open windows for posting such.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,840


Several things:

First setting the Subject of your post "BASE SAS" is totally non-descriptive and useless as a post Subject.

Secondly your post is one block of unreadable text, try applying some formatting, posting code in the code window (its the {i} above the post area) and using tabs and returns to format the text as you would when reading any material.

Finally, this looks like some sort of learning excercise.  Thus you should be learning this, if you have specific questions about, please be specific about them.

Super User
Posts: 13,941


And to through something back at whatever source of this project:

The requirement "Your task to write efficient SAS codes to answer the following questions:" does not provide how you are to measure "efficiency". Efficient could mean: minimize run time, minimize memory and/or disk space, make the code easy to maintain, robust to identify unexpected data conditions so that follow on processes are not started with incomplete/incompatible data and likely a few other things depending on the scope of a project.

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation