I have tens of thousands records shown in the below format. Example: one aggregate amount ($3200) for multiple invoice numbers (32, 33, 34, 35). Here is how I created by using Proc SQL:
PROC SQL;
CREATE TABLE INVENTORY AS
SELECT
DATE,
AMOUNT,
INVOICE
FROM DATA.DATA;
WHERE DATE = '31OCT2016'D;
QUIT;
| Date | Amount | Invoice # |
| 10/31/2016 | 3200 | 32, 33, 34, 35 |
Now the question is:
Is there any way that I could put the invoice # into different rows while keeping the aggregate amount of $3200 in the first row only? Like Below:
| Date | Amount | Invoice # |
| 10/31/2016 | 3200 | 32 |
| 10/31/2016 | 0 | 33 |
| 10/31/2016 | 0 | 34 |
| 10/31/2016 | 0 | 35 |
Thanks
This is easy in a data step.
You want to put an output statement into a loop where invoice=32, then 33, then 34, then 35. But after the first output, you want to set amount to a missing value. So the real issue is how to make a loop over the character variable INVOICELIST (="32,33,34,35"). This program does that:
Notes:
data have;
attrib date format=mmddyys10.
amount length=8
invoicelist length=$20;
input date mmddyy10. amount invoicelist;
datalines;
10/31/2016 3200 32,33,34,35
run;
data want;
set have;
do I=1 to countw(invoicelist,',');
invoice=input(scan(invoicelist,I,','),4.);
output;
amount=.;
end;
drop invoicelist;
run;
This is easy in a data step.
You want to put an output statement into a loop where invoice=32, then 33, then 34, then 35. But after the first output, you want to set amount to a missing value. So the real issue is how to make a loop over the character variable INVOICELIST (="32,33,34,35"). This program does that:
Notes:
data have;
attrib date format=mmddyys10.
amount length=8
invoicelist length=$20;
input date mmddyy10. amount invoicelist;
datalines;
10/31/2016 3200 32,33,34,35
run;
data want;
set have;
do I=1 to countw(invoicelist,',');
invoice=input(scan(invoicelist,I,','),4.);
output;
amount=.;
end;
drop invoicelist;
run;
You don't use DATALINES for large amounts of data. You read directly from an external file instead. Check out the INFILE statement.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.