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

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;

DateAmountInvoice #
10/31/2016320032, 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:

DateAmountInvoice #
10/31/2016320032
10/31/2016033
10/31/2016034
10/31/2016035

 

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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:

  1. Use the COUNTW function to find the number of "words" in invoicelist (where a word in this case is delimited by commas).
  2. Use the SCAN functino to retrieve those words in succession.

 

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;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

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:

  1. Use the COUNTW function to find the number of "words" in invoicelist (where a word in this case is delimited by commas).
  2. Use the SCAN functino to retrieve those words in succession.

 

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;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9
Thanks! I'm curious if I have huge amounts of data, do I have to put them all below the datalines statement?
SASKiwi
PROC Star

You don't use DATALINES for large amounts of data. You read directly from an external file instead. Check out the INFILE statement.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1065 views
  • 4 likes
  • 3 in conversation