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