sas >> adding rows in SAS dataset

by syam » Fri, 09 Feb 2007 06:49:56 GMT

Hi All,

I am posting this query with great expectation that somebody will help
me out. I have imported data from the text file in to SAS. The sample
abstract data of SAS dataset is given below:

Col1 Col2 Col3 Col4 Col5

CCS PSF 030-305 03073 RPOS
CCS PSF . . .
041-003 03072 Costs . .
CCS PSF 050-001 03056 Mail

Now the problem is that, I want to add the row 2 and 3 into single row
and want the final output to look as follows ( want to add the row 2
and 3 in the above sample data into one row)

Col1 Col2 Col3 Col4 Col5

CCS PSF 030-305 03073 RPOS
CCS PSF 041-003 03072 Costs ----> (row 2 and 3 are added)
CCS PSF 050-001 03056 Mail

It would be greatly appreciated if I get some help in writing SAS code
in this regards.

Thanks in advance.

Regards,
Syam


sas >> adding rows in SAS dataset

by Andrew H Karp » Fri, 09 Feb 2007 07:06:05 GMT


I think you need to look at PROC MEANS, a BASE SAS procedure that will
do what you require in this effort. Among other resources, you can
download, for free, a ZIP file from my web site called "Steps to
Success with PROC MEANS" that includes a powerpoint presentation, SAS
code and sample data sets.

Go to www.SierraInformation.com and click on the "past presentations"
link to get started.

Hope this helps...

Andrew Karp
Sierra Information Services
www.SierraInformation.com




> CCS PSF 030-305 03073>RPOS
> CCS PSF . > . .
> 041-003>03072 Costs . .
> CCS gt;>SF 050-001 03056 Mail
>
> Now the problem is that, I want to add >he row 2 and 3 into single row
> and want the final output to look as >ollows ( want to add the row 2
> and 3 in the >bo>e sample data into one row)
>
> C>l1>ol2 ol3 ol4 ol5
>> > CCS PSF 030-305 03073 RPO>
> CCS PSF 041-00> 03072 Costs ---> (row 2 and 3>ar> added)
> CCS PSF 050-001 03056 Mail
>
> It would be greatly>appreciated if I ge> s>me help in writing SA> c>de
> in th>s regards.

sas >> adding rows in SAS dataset

by syam » Fri, 09 Feb 2007 12:16:12 GMT


Hi Andrew,

The question is here that some of the rows in sas setdata were broken
into two rows. I want to merge only the broken rows into one row .

Thnaks
syam

sas >> adding rows in SAS dataset

by Arthur Tabachneck » Fri, 09 Feb 2007 12:23:10 GMT

Syam,

I'm not sure if I correctly understand what you are trying to do. Are
you trying to combine every 2nd and 3rd record? If so, then something
like the following might work:

data have;
input Col1 $ Col2 $ Col3 $ Col4 $ Col5 $;
cards;
CCS PSF 030-305 03073 RPOS
CCS PSF . . .
041-003 03072 Costs . .
CCS PSF 050-001 03056 Mail
;
run;

data want (rename=(newcol1-newcol5=col1-col5));
set have;
array col(*) col1-col5;
array newcol(5) $;
retain newcol;
if mod(_n_,3) eq 0 then do;
do i=3 to 5;
newcol(i)=col(i-2);
end;
output;
end;
else if mod(_n_,3) eq 1 then do;
do i=1 to 5;
newcol(i)=col(i);
end;
output;
end;
drop col: i;
run;

HTH,
Art
-----------

sas >> adding rows in SAS dataset

by syam » Fri, 09 Feb 2007 12:29:42 GMT

Hi Andrew,

Thanks a lot for ur quick response, let me try ur code on my original
dataset

Once again thanks a ton,
Syam

sas >> adding rows in SAS dataset

by ajs2004@bigfoot.com » Fri, 09 Feb 2007 18:28:09 GMT

> I have imported data from the text file in to SAS ...

You need to look at that text file and how you read it.

If you read the file with INFILE and INPUT, this looks like it's just
a case of using TRUNCOVER when you should have used FLOWOVER (which is
the default). The two almost-identical steps below illustrate - one
gives the wrong result you have, one gives the right result you want:

Obs col1 col2 col3 col4 col5

1 CCS PSF 030-305 03073 RPOS
2 CCS PSF
3 041-003 03072 Costs
4 CCS PSF 050-001 03056 Mail

Obs col1 col2 col3 col4 col5

1 CCS PSF 030-305 03073 RPOS
2 CCS PSF 041-003 03072 Costs
3 CCS PSF 050-001 03056 Mail

options nocenter;

data used_truncover;
input (col1 - col5) ($);
infile cards truncover;
cards;
CCS PSF 030-305 03073 RPOS
CCS PSF
041-003 03072 Costs
CCS PSF 050-001 03056 Mail
;
run;

proc print data=used_truncover;
title1 'with TRUNCOVER';
run;

data used_flowover;
input (col1 - col5) ($);
infile cards flowover;
cards;
CCS PSF 030-305 03073 RPOS
CCS PSF
041-003 03072 Costs
CCS PSF 050-001 03056 Mail
;
run;

proc print data=used_flowover;
title1 'with FLOWOVER';
run;

... which is far simpler than trying to write code to figure out when
records have been split and then splice them back together again.

ref http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/
a000146932.htm#a000167150

Similar Threads

1. add last row to dataset

Hi SAS-L,

See code below. Is there a more elegant way (within a single datastep?)
to add the last row (including the values 99 and "other") to table
mydata in order to obtain mydata2?

Arjen

data mydata;
input V F $;
cards;
1 D
. R
2 T
4 G
. H
5 Y
. Q
;
run;

data lastrow;
input V F $;
cards;
99 Other
;
run;

data mydata2;
set mydata lastrow;
run;

2. Fwd: calculate one row of a dataset, depending on another row

3. calculate one row of a dataset, depending on another row

Here is a simple solution,  others will have fancier programs.  This
method uses two sorts but if you don't have too many records the
inefficiency will not "cost" too much.

data work.cycle;
   input lngPatID Cycle dtmCouBeg:ddmmyy. @@;
   cards;
1 1 28.03.04  1 2 26.04.04  1 3 01.06.04
2 1 14.03.03  2 2 24.04.03
3 1 02.08.02  3 2 05.09.02  3 3 12.10.02  3 4 01.11.02
;;;;
   run;
proc sort data=work.cycle;
   by lngPatID descending cycle;
   run;
data work.cycle;
   set work.cycle;
   by lngPatID descending cycle;
   lagCouBeg = lag(dtmCouBeg);
   if first.lngPatId
      then dtmCouEnde = dtmCouBeg + 28;
      else dtmCouEnde = lagCouBeg - 1;
   dif = dtmCouEnde - dtmCouBeg;
   format dtm: ddmmyy10.;
   drop lagCouBeg;
   run;
proc sort data=work.cycle;
   by lngPatID cycle;
   run;
proc print;
   by lngPatID;
   id lngPatID;
   run;

On 4/13/06, Katrin Lehmann < XXXX@XXXXX.COM > wrote:
> Hello SAS freaks,
>
> I have the following dataset:
>
> lngPatID Cycle dtmCouBeg;
> 1          1    28.03.04
> 1          2    26.04.04
> 1          3    01.06.04
> 2          1    14.03.03
> 2          2    24.04.03
> 3          1    02.08.02
> 3          2    05.09.02
> 3          3    12.10.02
> 3          4    01.11.02
>
>
> what i need is:
>
> lngPatID Cycle dtmCouBeg  dtmCouEnde;
> 1          1    28.03.04  25.04.04
> 1          2    26.04.04  31.05.04
> 1          3    01.06.04  dtmCouBeg+28d
> 2          1    14.03.03  23.04.03
> 2          2    24.04.03  dtmCouBeg+28d
> 3          1    02.08.02  04.09.02
> 3          2    05.09.02  11.10.02
> 3          3    12.10.02  31.10.02
> 3          4    01.11.02  dtmCouBeg+28d;
>
> dtmCouEnde is for every lngPatID dtmCouBeg of the NEXT Cycle-1d.
> If it is the last Cycle for the lngPatID dtmCouEnde is dtmCouBeg of
> the SAME Cycle + 28d. My problem is, that I have really no idea, how to do
> this in a short way. So I'm very thankfully looking forward to your
> adwises.
>
> kind regards
> Katrin
>

4. SAS PROC FREQ not adding Rows/Columns Correctly

5. Dataset : variable with value to added in empty dataset

Hi All,
This is the example
data a;
input ID name $;
datalines;
;

data C;
set a;
if _n_=0;
var1="John";
run;

My situation is similar to this. I created a table from proc sql, where sql
query returns 0 records. After that I want add a variable with value. But
it is not working.

proc sql;
create table a as
select * from a;
quit;
 Now table a have 0 observations.


data a;
set a;
var1="Smith";
run;

Proc print;

returns 0 observations. I want Smith to be added in dataset a.
Help please. Any other way also welcome.

6. Header row of Dataset A as the first record of dataset B

7. adding obs to perm dataset using sas intr/net

Hello all, sorry if this is a repeat..i typed this before but got a
page could not be displayed error when i submitted..

I am trying to add observations to a permanent dataset using sas
intr/net. To test this i have created a simple html form with one
field.."Name". When a name is entered and submitted i am hoping it
gets passed to a permanent dataset where additional entries can be
added as they are input into the html form..I think i have the correct
code..(see below)..the problem is, i am getting an error message
stating that i do not have write priveleges for the library..How do i
configure write priveleges? or are there any suggestions you have
where i can accomplish this task without write priveleges? Thanks,
Nevin

%global nam;
data work.test;
name=symget("nam");
run;

libname SAMPDAT '!SASROOT\intrnet\sample';
data sampdat.dataset;
	set work.test;
run;

ods listing close;
ods html body=_webout rs=none style=sasweb;
 proc print data=work.test;
 run;
ods html close;
ods listing;

8. Insert one row in SAS dataset