DATA Step, Macro, Functions and more

Is there any way to separate multiple records from one cell into one record per row?

Accepted Solution Solved
Reply
Contributor LL5
Contributor
Posts: 41
Accepted Solution

Is there any way to separate multiple records from one cell into one record per row?

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

 

 


Accepted Solutions
Solution
‎11-22-2016 08:05 AM
Valued Guide
Posts: 797

Re: Is there any way to separate multiple records from one cell into one record per row?

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;

 

 

View solution in original post


All Replies
Solution
‎11-22-2016 08:05 AM
Valued Guide
Posts: 797

Re: Is there any way to separate multiple records from one cell into one record per row?

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;

 

 

Contributor LL5
Contributor
Posts: 41

Re: Is there any way to separate multiple records from one cell into one record per row?

Thanks! I'm curious if I have huge amounts of data, do I have to put them all below the datalines statement?
Super User
Posts: 3,113

Re: Is there any way to separate multiple records from one cell into one record per row?

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

Contributor LL5
Contributor
Posts: 41

Re: Is there any way to separate multiple records from one cell into one record per row?

I see. Got it. Thanks.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 210 views
  • 4 likes
  • 3 in conversation