BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5

yes i have tried but not working

data foo;

length slurp $32000;

retain slurp '';

input @;

slurp=cats(of slurp _infile_);

if not mod(countw(slurp,'|'),3) then

  do;

   do i=1 to countw(slurp,'|') by 3;

    name=input(scan(slurp,i,'|'),$32.);

    user=input(scan(slurp,i+1,'|'),$32.);

    no=input(scan(slurp,i+2,'|'),8.);

    output;

   end;

   slurp='';

  end;

drop slurp i;

cards;

ajay|ku

mar|2656

muni|carr

om|455676

ajay|kumar|9890|vijay|kumar|589|user|flow|568

;

run;

This is ok but i wnat it with outcards to insert as i am having 70000 obs i cant insert ,can u create dataset fisrt and after that we can use the code

data temp;
infile cards missover truncover;
input sclurp $32000. ;
cards;
ajay|ku
mar|2656

      
muni|carr
om|455676

       
ajay|kumar|9890|vijay|kumar|589|

user|flow|568
    
run;

data temp2;
/*input @;*/
set temp;
sclurp=cats(of sclurp );
retain sclurp '';
if not mod(countw(sclurp,'|'),4) then
  do;
   do i=1 to countw(sclurp,'|') by 4;
    name=input(scan(sclurp,i,'|'),$32.);
    user=input(scan(sclurp,i+1,'|'),$32.);
    no=input(scan(sclurp,i+2,'|'),8.);
        output;
   end;
   sclurp='';
  end;
drop sclurp i;

run;
proc print;
run;

as mentioned below i am trying by this way but i am not getting the output as getting ERROR as cards or infile is no there art can u help me in this.

Thqs in advance

art297
Opal | Level 21

Same difference, but the code has to be changed if you really only have name,name,number.  The following was written to account for the data you provided that had 2 names and 2 numbers in each record:

data _NULL_ ;

  FILE "c:want.txt" RECFM=N ;

  infile "c:\mydata.txt";

  input;

  do i=1 to length(_infile_);

    dummy=substr(_infile_,i,1);

    if dummy eq "|" then counter+1;

    if counter eq 3 and anyalpha(dummy) then do;

      put "|";

      counter+1;

    end;

    if counter eq 4 then counter=0;

    IF dummy GT '13'X THEN do;

      PUT dummy $CHAR1. ;

    end;

  end;

run;

data want;

  infile "c:\want.txt" dlm="|" dsd;

  informat first last $15.;

  input first last number1 number2 @@;

run;

Peter_C
Rhodochrosite | Level 12

Hi i am getting data in txt file with having Enter(not word it means going to next line)

i am having the data like this as i am having 72 variables and 80000 obs


ajay|ku
mar|2656|
muni|carr
om|455676|
ajay|kumar|9890|vijay|kumar|589|user|flow|568

i want the output

Name user no
ajay kumar 2656
muni carrom 455676
ajay kumar 9890
vijay kumar 589
user flow 568


surely that is simple

"enter" is the default end-of-line character SAS expects for simple (even delimited) input.

The step is just

data your_data( compres= yes ) ;

infile 'your_text_file' DSD dlm= '|' lrecl=8000 truncover ;

input ( col1-col72)( :$30. ) ;

run ;

Tom
Super User Tom
Super User

I would use something like this to convert to one row per observation so that it can be read using DSD option on an INFILE statement. 

This program removes all of the CR/LF characters and insert a line break after every third vertical bar that it reads. 

To use for your large file with 72 variables then change the 3 to a 72 in the MOD() function call. 

Note that is is dependent on the vertical bars that I see on the end of the lines.  If those are not there then the logic will need to be more complex.

data _null_;

  infile tmpfile1 recfm=f lrecl=1;

  file tmpfile2 lrecl=2000 ;

  input char $char1. ;

  if char in ('0d'x,'0a'x) then delete;

  if (char='|') then do;

    words+1;

    if 0=mod(words,3) then put ;

    else put char $char1. @;

  end;

  else put char $char1. @;

run;

Input file

ajay|ku

mar|2656|

muni|carr

om|455676|

ajay|kumar|9890|vijay|kumar|589|user|flow|568

Output file

ajay|kumar|2656

muni|carrom|455676

ajay|kumar|9890

vijay|kumar|589

user|flow|568

R_Win
Calcite | Level 5

But what is where to take the input file you have given two input files for example my file path is

'c/longdata.txt' i have kept it at infile1 and what about file you have given tmpfile2.

infile tmpfile1 recfm=f lrecl=1;

  file tmpfile2 lrecl=2000 ;

Tom
Super User Tom
Super User

To use the code I submitted you need to either define the filenames or just change the FILE and INFILE statements to reference the physical files you have and/or want.  If you do not want to save the converted version of the data file then you can use the TEMP engine on the FILENAME statement for TMPFILE2.

(You can also use other filerefs that might be more meaningful to you than TMPFILE1 and TMPFILE2, I just used those as examples.)

filename tmpfile1 'c/longdata.txt';

filename tmpfile2 'c/longdata_fixed.txt';

/* Insert the code to convert from one file format to the other here. */

* Now read in the more normal looking file into your actual SAS dataset ;

data want;

   infile tmpfile2 dsd dlm='|' truncover ;

  length name1 name2 $12 value 8 ;

   input name1 name2 value ;

run;

Ksharp
Super User

OK.

data want(keep=name user no );
infile 'c:\x.txt' lrecl=2000 pad ;
length name user no temp $ 40 b $ 1;
input a $char1. @@;
retain temp name user no b;
if a ne '|' then temp=cats(temp,a);
if a='|' or (anydigit(b) and notdigit(a)) then do;
                                       count+1;
                                       select(mod(count,3));
                                         when (1) name=temp;
                                         when (2) user=temp;
                                         when (0) do; no=temp;output;end;
                                        end;
                                        call missing(temp);
                                        end;
b=a;
run;


















data want(keep=name user no );
infile datalines ;
length name user no temp $ 40 b $ 1;
input a $char1. @@;
retain temp name user no b;
if a ne '|' then temp=cats(temp,a);
if a='|' or (anydigit(b) and notdigit(a)) then do;
                                       count+1;
                                       select(mod(count,3));
                                         when (1) name=temp;
                                         when (2) user=temp;
                                         when (0) do; no=temp;output;end;
                                        end;
                                        call missing(temp);
                                        end;
b=a;
datalines;
ajay|ku
mar|2656|
muni|carr
om|455676|         
ajay|kumar|9890|vijay|kumar|589|user|flow|568
nani|kumar
    |20
kumar|raju|45|arun|kumar|78
n
aresh|k|45
;
run;




Ksharp

R_Win
Calcite | Level 5

khsarp i have tryed your code i have started waited for 15 mins it has read 600 records only as i am having 80000 records any help

Thqs in advance

Ksharp
Super User

It is hard to give you some constructive suggestion without seeing all of your data .

Peter_C
Rhodochrosite | Level 12

R_Win

I have a data step tested below on your simple data. I cannot tell if it will work on your  real data of 72 columns.

It works by concatenating lines until one ends with the | delimiter. There may be another way if this is not successful. Try it:

(code formatting is supplied by EG4,3 )

* first, need an almost dummy file ;

filename dum "%sysget(mysasfiles)\trouble.x";

data _null_;

   file dum;

* that isn't empty;

   put;

run;

data broken( keep = name user no );

   infile cards truncover eof=close ;

   length my_buffer $32767;

   retain my_buffer;

   input @;

   my_buffer = catt( my_buffer, _infile_ );

   if substr( my_buffer , length(my_buffer) ) = '|' then

      do;

         link read_some;

          * clear the buffer ready for next inputs ;

          my_buffer = ' ';

         delete;

      end;

   return;

   close: if my_buffer = ' ' then delete;

   read_some :

      * now read that buffer until all consumed;

   length name $30 user $8 no 8;

   infile dum dlm= '|' dsd truncover column=c;

         * option column=c provides position in variable C for next INPUT ;

   * position pointer to column 1 ready for first INPUT  ;

   input @1 @@;

   _infile_ = my_buffer;

   len = length( my_buffer);

   do while( c LT len );

   * do while pointer within the data in the buffer  ;

      input name user no @@;

      if catt( name,user,no) ne '.' then

         output;

   end;

   return;

   cards;

ajay|ku

mar|2656|

muni|carr

om|455676|         

ajay|kumar|9890|vijay|kumar|589|user|flow|568|

;

and the results

FSVIEW:  WORK.BROKEN (B)----------------------------------------

Obs     name                            user                no

                                                               

   1     ajay                            kumar             2656

   2     muni                            carrom          455676

   3     ajay                            kumar             9890

   4     vijay                           kumar              589

   5     user                            flow               568

hope it will be useful

peterC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 24 replies
  • 1320 views
  • 0 likes
  • 6 in conversation