DATA Step, Macro, Functions and more

How to - Separate .Txt File While Import to EG

Accepted Solution Solved
Reply
Super Contributor
Posts: 381
Accepted Solution

How to - Separate .Txt File While Import to EG

Hello everyone,

 

I found a SAS document related to VIEW then I executed in my SAS EG locale. I think It needs to be 5 rows instead of 4 rows but I couldn’t see the the row which begins with frisbee. On the other hand, my another question is how to take student.txt file as following image, how should I separate it when import to EG. Does anyone here can show me with screenshot ? If you show me over student.txt file so much the better.

 

libname myv9lib 'C:\Try';
filename student 'C:\Try\student.txt';
data myv9lib.class(keep=name major credits)
   myv9lib.problems(keep=code date) / view=myv9lib.class;
infile student;
   input name $ 1-10 major $ 12-14 credits 16-18;
select;
when (name=' ' or major=' ' or credits=.)
         do code=01;
            date=datetime();
            output myv9lib.problems;
         end; 
when (0<credits<90)
         do code=02;
            date=datetime();
            output myv9lib.problems;
         end; 
otherwise
       output myv9lib.class;
   end;
run; 
proc print data=myv9lib.class;
run;  

proc print data=myv9lib.problems;
   format date datetime18.;
run;

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001278887.htm  

student.png

 

Thanks


Accepted Solutions
Solution
‎01-04-2016 06:37 PM
Occasional Contributor
Posts: 6

Re: How to - Separate .Txt File While Import to EG

[ Edited ]

Hi,

 

Your every thing perfect except "Missover" option missed in your Infile statement.

 

Try It... I got the results as you expected.

 

First Question:

 

data myv9lib.class1(keep=name major credits)
   myv9lib.problems1(keep=code date) / view=myv9lib.class1;
infile student missover;
   input name $ 1-10 major $ 12-14 credits 16-18;
select;
when (name=' ' or major=' ' or credits=.)
         do code=01;
            date=datetime();
            output myv9lib.problems1;
         end; 
when (0<credits<90)
         do code=02;
            date=datetime();
            output myv9lib.problems1;
         end; 
otherwise
       output myv9lib.class1;
   end;
run; 
proc print data=myv9lib.class1;
run;  

proc print data=myv9lib.problems1;
   format date datetime18.;
run;

 

Second Question:

 

data class(keep=name major credits);
infile student missover;
   input name $ 1-10 major $ 12-14 credits 16-18;
run; 

Thanks & Regards,

Bharath

View solution in original post


All Replies
Solution
‎01-04-2016 06:37 PM
Occasional Contributor
Posts: 6

Re: How to - Separate .Txt File While Import to EG

[ Edited ]

Hi,

 

Your every thing perfect except "Missover" option missed in your Infile statement.

 

Try It... I got the results as you expected.

 

First Question:

 

data myv9lib.class1(keep=name major credits)
   myv9lib.problems1(keep=code date) / view=myv9lib.class1;
infile student missover;
   input name $ 1-10 major $ 12-14 credits 16-18;
select;
when (name=' ' or major=' ' or credits=.)
         do code=01;
            date=datetime();
            output myv9lib.problems1;
         end; 
when (0<credits<90)
         do code=02;
            date=datetime();
            output myv9lib.problems1;
         end; 
otherwise
       output myv9lib.class1;
   end;
run; 
proc print data=myv9lib.class1;
run;  

proc print data=myv9lib.problems1;
   format date datetime18.;
run;

 

Second Question:

 

data class(keep=name major credits);
infile student missover;
   input name $ 1-10 major $ 12-14 credits 16-18;
run; 

Thanks & Regards,

Bharath

Super Contributor
Posts: 381

Re: How to - Separate .Txt File While Import to EG

Hello ,

 

Thank you for your help. About my second question, I have to say that, I want to see an image while we import the file from .txt to EG to understand better.I shared a  image as below. I’m not sure what should I do in the following image to get the data set as my previous message's image.

http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a002645812.htm

 

SecondQuestion.png

 

About the MISSOVER option, I found a website link. Actually, I nearly understood the option but by default how data reads to raw data from .txt ? How did the following output happen? In spite of following writing I couldn’t properly get it.

 

The DATA step reads the first value (22). Because the value is shorter than the 5 characters expected by the informat,(This part is ok.) the DATA step attempts to finish filling the value with the next record (333).(Why 333 ??? It is also shorter than the 5 characters )This value is entered into the PDV and becomes the value of the TestNumber variable for the first observation.

 

How.png

 

 

Thank you.

Super User
Posts: 17,823

Re: How to - Separate .Txt File While Import to EG

Based on the code, you're not dealing with a delimited file, you have a fixed file. Change it from delimited to fixed columns.
Super Contributor
Posts: 381

Re: How to - Separate .Txt File While Import to EG

Hello @Reeza,

 

Happy new year to you. I've already tried fixed selection but I couldn't get it.I put a image as below. What should I write now ?  It is not necessary but I just want to learn it. Can you lead me over the image, please ?

 

Fixed.png

 

Thank you.

Super User
Posts: 17,823

Re: How to - Separate .Txt File While Import to EG

Well for starters your record length is 20, and your lines are 18, so try changing that.
Super Contributor
Posts: 381

Re: How to - Separate .Txt File While Import to EG

Hello @Reeza,

I've actually tried 18 also I put a image as below it includes the screen after I wrote 18 in "Fixed Colum" textbox. Do you have a chance to try the srudent.txt ? But it is not necessarily .Thanks though.

 

18.png

Respected Advisor
Posts: 3,890

Re: How to - Separate .Txt File While Import to EG

The columns are of fixed length but they are not all of the same length so you can't just set a value of 18 or 20.

 

As shown below the EG import wizard lets you set different lengths simply by clicking on the right position in the preview grid. This will result in the correct SAS code being generated. See example below

Capture.PNG

Super Contributor
Posts: 381

Re: How to - Separate .Txt File While Import to EG

Hello @Patrick,

 

When I selected the "Fixed Colum" radio button last time "Record Length" was enable. Now the "Record Length" is unable. I don't understand the reason.  Thank you for your help.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 512 views
  • 2 likes
  • 4 in conversation