BookmarkSubscribeRSS Feed
Cheggg1111
Calcite | Level 5

I have a file of raw data(given below) and I am hoping to turn it in to the output (given below) by using a single data step.

Each observation contains only the following variables: the salesperson’s name (character type of length at least 20), the salesperson’s latest sales date (in SAS DATE numeric values), the number of days (standard numeric type) between the salesperson’s latest date of sales and the 30th of September 2021 (For example, if the latest sales date is 29/Sep/2021, then the number of days between the latest date of sales and the 30th of September 2021 is 1.), and the salesperson’s total sales (standard numeric type). 

Raw Data:

David Wong,1/Sep/2021,13200

Brian Leung,15/Sep/2021,23450

Brian Leung,10/Sep/2021,33000

Mary Chan,3/Sep/2021,45600

Mary Chan,20/Sep/2021,37800

Mary Chan,1/Aug/2021,21500

Mary Chan,30/Aug/2021,42000

John Tam,12/Sep/2021,35000

 

Output:

Cheggg1111_1-1635632071887.png

 

 

5 REPLIES 5
Cheggg1111
Calcite | Level 5

How to I get the output (given below) from the raw data (given) by only using one Data Step.

Raw Data:

David Wong,1/Sep/2021,13200

Brian Leung,15/Sep/2021,23450

Brian Leung,10/Sep/2021,33000

Mary Chan,3/Sep/2021,45600

Mary Chan,20/Sep/2021,37800

Mary Chan,1/Aug/2021,21500

Mary Chan,30/Aug/2021,42000

John Tam,12/Sep/2021,35000

Each observation contains only the following variables: the salesperson’s name (character type of length at least 20), the salesperson’s latest sales date (in SAS DATE numeric values), the number of days (standard numeric type) between the salesperson’s latest date of sales and the 30th of September 2021 (For example, if the latest sales date is 29/Sep/2021, then the number of days between the latest date of sales and the 30th of September 2021 is 1.), and the salesperson’s total sales (standard numeric type).

Output:

Cheggg1111_1-1635632934651.png

 

 

 

Patrick
Opal | Level 21

Welcome to the SAS Forum.

Going forward please post your questions only once.

 

What have you tried already?

 

Here some pointers:

1. Your source data must be sorted by salesperson and date (if that's not the case the use Proc Sort)

2. The data must be read into SAS as a SAS Date value (use the appropriate SAS date informat in the input step, apply a SAS date format to the variable).

3. Use by salesperson name and first/last logic in the data step

- sales_tot+sales

- if last.salesperson then do; no_days=sales_date - '30sep2021'd;output; call missing(sales_tot); end;

 

You can find an answer with code here - but suggest you give this first a try on your own.

Astounding
PROC Star
This can be done (but not by me) in a single DATA step, using unsorted raw data, using a hash table that uses NAME as the key.

Results would be in alphabetical order.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 780 views
  • 0 likes
  • 4 in conversation