BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shubham1
Calcite | Level 5

hello

 

i have a text  file which contain records like this 

 

file have header with table name ,physical name ,descripition ,type all the records are seprtaed by a delimliter # 

 

description is mentioned in multiple lines

 

tablename,physical_name,descripition,type


T1#customer_data# this is a customer
table with different data
and records which is used
for extraction
#char
T2#accounts# this is account information
which contain accout number and
account details as well
as other
information#char

 

i want to create a dataset 

table, physical name,description and type 

 

How can I read this file 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

I created a file in my sascommunity test directory with these contents:

tablename,physical_name,descripition,type

T1#customer_data# this is a customer
table with different data
and records which is used
for extraction
#char
T2#accounts# this is account information
which contain accout number and
account details as well
as other
information#char

I then used this code from the link I gave you:

filename sample '$HOME/sascommunity/shubham.txt';

filename fixed '$HOME/sascommunity/shubham_f.txt';

data _null_;
  retain maxp 3 p 0;
  infile sample firstobs=3;
  file fixed ;
  input ;
  if p then put ' ' @;
  put _infile_ @;
  p+countc(_infile_,'#');
  
  if p>= maxp then do ;
    put ;
    p=0;
  end;
run;

Note that there are only four alterations:

  1. the output file is not temporary
  2. the number of expected delimiters is fixed (in the retain statement)
  3. the statement determining the maximum number of delimiters in _n_=1 has been removed
  4. the delimiter has been changed to the hash sign

After that, I ran the following import:

data want;
infile fixed dlm='#' truncover;
input
  tablename :$2.
  physical_name :$32.
  description :$200.
  type :$10.
;
run;

This is how the resulting dataset looks like:

T1	customer_data	this is a customer table with different data and records which is used for extraction	char
T2	accounts	this is account information which contain accout number and account details as well as other information	char

 

View solution in original post

12 REPLIES 12
ChrisNZ
Tourmaline | Level 20

Please show us an example of what the text data actually looks like (alter the values if needed).

Use the {i} icon to paste a couple of example records.

Also show what the expected output looks like for these 2 records.

 

 

shubham1
Calcite | Level 5

this is how dataset look like

 

Capture.JPG

Kurt_Bremser
Super User

Please re-read @ChrisNZ's post carefully and thoroughly, and act accordingly. Do NOT post data in pictures. Expecting us to type data off pictures into code is thoughtless at best, but usually considered a sign of laziness, being impolite or plain rude.

If you need help, help us in helping you.

 


@shubham1 wrote:

this is how dataset look like

 

Capture.JPG


 

shubham1
Calcite | Level 5

this is how I want output to look like 

 

table_name physical_name descripition type
T1,customer_data,this is a customer table with different dataand records which is used for extraction,char
T2,accounts,this is account information which contain accout number and account details as well as other information,char

Kurt_Bremser
Super User

So your external file has this layout:

T1,customer_data,this is a customer table with different dataand records which is used for extraction,char
T2,accounts,this is account information which contain accout number and account details as well as other information,char

Note how posting text with the {i} icon works. Fixed font, and no possible conversion of special characters.

Try this code for reading it:

data want;
infile cards dlm=',';
input
  tablename :$2.
  physical_name :$32.
  description :$200.
  type :$10.
;
cards;
T1,customer_data,this is a customer table with different dataand records which is used for extraction,char
T2,accounts,this is account information which contain accout number and account details as well as other information,char
;
run;

Note how code posted with the little running man icon looks like. Almost like in the SAS enhanced editor.

For your practical purpose, remove the cards; section and replace the cards keyword with the physical filename of yout infile.

 

 

shubham1
Calcite | Level 5

I am having an txt file which looks like this  where records are seprated by # and i want these records to come in four variable in SAS dataset 

table_name,physical_name,descripition and type .In the text file records are starting from line 2 

 

tablename,physical_name,descripition,type

T1#customer_data# this is a customer
table with different data
and records which is used
for extraction
#char
T2#accounts# this is account information
which contain accout number and
account details as well
as other
information#char

 

i want this txt file to come into a SAS dataset 

 

table_name physical_name descripition type
T1,customer_data,this is a customer table with different data and records which is used for extraction,char
T2,accounts,this is account information which contain account number and account details as well as other information,char

shubham1
Calcite | Level 5
tablename,physical_name,descripition,type

T1#customer_data# this is a customer
table with different data
and records which is used
for extraction
#char
T2#accounts# this is account information
which contain accout number and
account details as well
as other
information#char
Kurt_Bremser
Super User

If you have line breaks within data that should be a single column, you need to remove these first.

SAS will act on line breaks BEFORE it acts on delimiters, thereby scrambling your file layout.

 

Searching here on the communities for "line breaks in variables" found me this (currently the 7th hit): https://communities.sas.com/t5/General-SAS-Programming/Carriage-Return-Multiple-Line-Break-Issue-Imp...

It has a solution for a similar issue, although the deilimiters are pipes instead of hash signs.

shubham1
Calcite | Level 5

i am not getting how can i use the code that is present in this link to read my txt file 

 

https://communities.sas.com/t5/General-SAS-Programming/Carriage-Return-Multiple-Line-Break-Issue-Imp...

Kurt_Bremser
Super User

I created a file in my sascommunity test directory with these contents:

tablename,physical_name,descripition,type

T1#customer_data# this is a customer
table with different data
and records which is used
for extraction
#char
T2#accounts# this is account information
which contain accout number and
account details as well
as other
information#char

I then used this code from the link I gave you:

filename sample '$HOME/sascommunity/shubham.txt';

filename fixed '$HOME/sascommunity/shubham_f.txt';

data _null_;
  retain maxp 3 p 0;
  infile sample firstobs=3;
  file fixed ;
  input ;
  if p then put ' ' @;
  put _infile_ @;
  p+countc(_infile_,'#');
  
  if p>= maxp then do ;
    put ;
    p=0;
  end;
run;

Note that there are only four alterations:

  1. the output file is not temporary
  2. the number of expected delimiters is fixed (in the retain statement)
  3. the statement determining the maximum number of delimiters in _n_=1 has been removed
  4. the delimiter has been changed to the hash sign

After that, I ran the following import:

data want;
infile fixed dlm='#' truncover;
input
  tablename :$2.
  physical_name :$32.
  description :$200.
  type :$10.
;
run;

This is how the resulting dataset looks like:

T1	customer_data	this is a customer table with different data and records which is used for extraction	char
T2	accounts	this is account information which contain accout number and account details as well as other information	char

 

ChrisNZ
Tourmaline | Level 20

Like this?

data _null_;
file 'c:\temp\t.txt';
put 'tablename,physical_name,descripition,type  ';
put '                                             ';
put 'T1#customer_data# this is a customer         ';
put 'table with different data                    ';
put 'and records which is used                    ';
put 'for extraction                               ';
put '#char                                        ';
put 'T2#accounts# this is account information     ';
put 'which contain accout number and              ';
put 'account details as well                      ';
put 'as other                                     ';
put 'information#char                             ';
run;

data tt;
  infile 'c:\temp\t.txt' firstobs=3;
  input ;                  
  retain DATASET NAME DESC;
  length DATASET NAME DESC $200;
  if countc(_infile_,'#')=2 then do;
    DATASET=scan(_infile_,1,'#');
    NAME   =scan(_infile_,2,'#');
    DESC   =scan(_infile_,3,'#');
  end;
  else if _infile_ ne '#char' then DESC=catx(' ',DESC,scan(_infile_,1,'#'));
  if index(_infile_, '#char') then output;
run;
proc print; run;

 

Obs DATASET NAME DESC
1 T1 customer_data this is a customer table with different data and records which is used for extraction
2 T2 accounts this is account information which contain accout number and account details as well as other information

 

Please provide complete and clear explanation in your first post next time, and fully answer the questions asked. Don't make it hard to help you.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1560 views
  • 0 likes
  • 3 in conversation