Help using Base SAS procedures

Reg:Split obs

Reply
Contributor
Posts: 37

Reg:Split obs

Data temp;

infile cards dlm="|";

input bcode$ Rcode $;

cards;

tcs,satyam|123,456

|

rnrl|567

run;

actually if bode is having two obs separated by comma it shoukld go in to two rows and rcode is having two obs separated by , should go to 2 rows

if both are balank it should have single rom  and if it is having only one obs it should be as it is.

output

bcode Rcode

tcs      null

satam  null

    null      123

   null      456

<only one balnk row shuld come as both are blank for both the values>      

rnrl        null

  null        567

i have attched print screen for your ref..

Contributor
Posts: 37

Reg:Split obs

Data temp;

infile cards dlm="|";

input bcode$ Rcode $;

cards;

tcs,satyam|123,456

|

rnrl|567

run;

actually if bode is having two obs separated by comma it shoukld go in to two rows and rcode is having two obs separated by , should go to 2 rows

if both are balank it should have single rom  and if it is having only one obs it should be as it is.

output

bcode Rcode

tcs      null

satam  null

    null      123

   null      456

null       null   

rnrl        null

  null        567

Frequent Contributor
Posts: 104

Re: Reg:Split obs

Two fixes:

1) declare the length of the variables up front, don't let SAS guess at it.

2) you need the TRUNCOVER option so SAS doesn't flow over to the next line looking for information

Data temp;

    length bcode rcode $ 12;

    infile cards dlm="|" truncover;

    input bcode $ Rcode $;

cards;

tcs,satyam|123,456

|

rnrl|567

run;

Also posting in the "SAS macro, data step..." section is more appropriate as you are not asking a question about a SAS PROC.

Super User
Posts: 9,662

Re: Reg:Split obs

How about:

Data temp;
infile datalines length=len;
input temp $varying200. len;
datalines;
tcs,satyam|123,456
|
rnrl|567
;
run;
data want(keep=bcode rcode);
 set temp;
 length bcode rcode $ 40;
 a=scan(temp,1,'|');
 b=scan(temp,2,'|');
 if missing(a) and missing(b) then do;
                                     call missing(bcode,rcode);
                                     output;
                                   end;
    else do;
           i=1; bcode=scan(a,i,',');
           do while (not missing(bcode));
            output;
            i+1;
            bcode=scan(a,i,',');
           end;

           j=1;rcode=scan(b,j,',');
           do while (not missing(rcode));
            output;
            j+1;
            rcode=scan(b,j,',');
           end;
         end;
run;

Ksharp

Ask a Question
Discussion stats
  • 3 replies
  • 152 views
  • 0 likes
  • 3 in conversation