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

Hi Everyone,

 

I am trying to export my SAS Dataset into text file.

i Tried 3 ways

 

1) I tried the export menu option by clicking the file and the resulting text file showed some weird sign as under 

clamno lineno freqcode ICN
0101011521587 001 8 " 1502141198141"
0101011521587 002 8 " 1502141198141"
0101011521587 003 8 " 1502141198141"
0101011521587 004 8 " 1502141198141"
0101011521587 005 8 " 1502141198141"
0101011521587 006 8 " 1502141198141"

 

I don't need the ( " ) marks around " 1502141198141"

 

2) i wrote a proc export command as under 

 

proc export data= import
outfile='C:\Users\athapa\Desktop\cc.txt'
dbms= tab replace;
run;

The output is 

 

clamno lineno freqcode ICN
0101011521587 "001 " 8 " 1502141198141"
0101011521587 "002 " 8 " 1502141198141"
0101011521587 "003 " 8 " 1502141198141"
0101011521587 "004 " 8 " 1502141198141"
0101011521587 "005 " 8 " 1502141198141"
0101011521587 "006 " 8 " 1502141198141"

Again the weird quotations around numbers 

 

3) I tried the following code 

data _null_ ;
file "C:\Users\athapa\Desktop\ICN\aaa.txt" delimiter= tab ;
Set import;
if _n_ = 1 then do;
put "clamno" tab
"lineno" tab
"claimfrq" tab
"icn"
;
end;
Put (_all_)(+0);
;
run;

The output this time looks ok but has a lot of spacing, which i do not need. i only need 1 single space between variables.

clamno lineno claimfrq icn
0101011521587 001 8  1502141198141
0101011521587 002 8  1502141198141
0101011521587 003 8  1502141198141
0101011521587 004 8  1502141198141
0101011521587 005 8  1502141198141
0101011521587 006 8  1502141198141
0101011521587 007 8  1502141198141

 

I have attached the datase HAVE with only 10 obs. The actual data has 6111 obs. Since i could not post my sas dataset here. I have attached a text file of the Have sas dataset.

So first we have import this HAVE text file and then export it as text file.

 

Could someone please help me to understand where i am going wrong.

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

If it is not the tab that makes the long space, probably the spaces are part of the columns.

One more try, add a length statemane before the infile in order to truncate the value ti desired length:

 

data import;

length clamno $13 lineno $1 freqcode $1 icn $13;
infile "C:\Users\athapa\Desktop\ICN\QI_30_Medicare_01Dec2016.txt" truncover firstobs = 2 ;
input clamno $ 1-13 lineno $ 15-18 @19 freqcode $1. ICN $ 20-40;
run;

 

Alternatively you may change the INPUT statemnet:

input @1 clamno $13. @15. lineno  $1. @19 $1.  @27 icn $13. ;

 

Is lineno just one digit? can it be longer?  if yes - adapt its length to the maximum available digits ($n. instead $1.)

I freqcode just one digit ? - if need adapt length to maximum digits.

View solution in original post

10 REPLIES 10
Shmuel
Garnet | Level 18

All your 4 variables are numeric. The ouput is outcome of formats related to them.

You should decide how long should be each variable on output and do yo want leading zeroes.

The first ant the 4th variables should use, probably, format  z13.

while the 2nd and 3rd to be of one dizgit (?!) - then format should be z1. (z + number of digits inclde leading zeroes)

 

Why are you using delimeter tab ? that may make the impression of many blanks between variables.

 

Your code should be:

data _null_ ;
file "C:\Users\athapa\Desktop\ICN\aaa.txt" delimiter= ' ' ;
Set import;
if _n_ = 1 then do;
put "clamno" tab
"lineno" tab
"claimfrq" tab
"icn"
;
end;
Put  calmno Z13.  ' ' lineno Z4. ' ' claimfrq  Z2. ' '  icn z13. ;   /* adapt length if need */
;
run;

 

athapa1183
Obsidian | Level 7

Sorry for late reply.

The variables are all character (They need to be character variables )as i imported them as under

 

data import;
infile "C:\Users\athapa\Desktop\ICN\QI_30_Medicare_01Dec2016.txt" truncover firstobs = 2 ;
input clamno $ 1-13 lineno $ 15-18 @19 freqcode $1. ICN $ 20-40;
run;

I don't want them to be numeric so i cannot use Z. format.

 

I am using tab as i am assuming there is a space between all 4 variables as shown by code above 

sample of exported text file aaa

clamnolineno freqcodeICN
0101011521587 001 8 1502141198141
0101011521587 002 8 1502141198141
0101011521587 003 8 1502141198141
0101011521587 004 8 1502141198141
0101011521587 005 8 1502141198141
0101011521587 006 8 1502141198141
0101011521587 007 8 1502141198141

even though it looks fine here .In my text file i still can see many spaces between 8 & 1502141198141. 

the problem still remains why there are so many spaces.

The code is still doesn't help.

Thanks

Shmuel
Garnet | Level 18
##- Please type your reply above this line. Simple formatting, no
attachments. -##
The long space is the result of tabing in your text brouser/editor.
Replace the tab with any charscter, including blank, and check again
athapa1183
Obsidian | Level 7

 

i did this 

 

data _null_ ;
file "C:\Users\athapa\Desktop\ICN\aaa.txt" delimiter= '' ;
Set avis.have;
if _n_ = 1 then do;
put "clamno" ''
"lineno" ''
"claimfrq" ''
"icn"
;
end;
Put (_all_)(+0);
;
run;

 

still same results.

Peter_C
Rhodochrosite | Level 12
Try file statement option DSD with DLM=" "
and adapt your PUT statement to
PUT (_all_)(:) ;
Peter_C
Rhodochrosite | Level 12
Pesky formatting
PUT (_all_)( : ) ;
athapa1183
Obsidian | Level 7

sorry for late reply.

sample of exported text file aaa

clamnolineno freqcodeICN
0101011521587 001 8 1502141198141
0101011521587 002 8 1502141198141
0101011521587 003 8 1502141198141
0101011521587 004 8 1502141198141
0101011521587 005 8 1502141198141
0101011521587 006 8 1502141198141
0101011521587 007 8 1502141198141

even though it looks fine here .In my text file i still can see many spaces between 8 & 1502141198141. 

the problem still remains why there are so many spaces.

 

All the variables are character variables because i imorted them that way as under

 

data import;
infile "C:\Users\athapa\Desktop\ICN\QI_30_Medicare_01Dec2016.txt" truncover firstobs = 2 ;
input clamno $ 1-13 lineno $ 15-18 @19 freqcode $1. ICN $ 20-40;
run;

Shmuel
Garnet | Level 18

If it is not the tab that makes the long space, probably the spaces are part of the columns.

One more try, add a length statemane before the infile in order to truncate the value ti desired length:

 

data import;

length clamno $13 lineno $1 freqcode $1 icn $13;
infile "C:\Users\athapa\Desktop\ICN\QI_30_Medicare_01Dec2016.txt" truncover firstobs = 2 ;
input clamno $ 1-13 lineno $ 15-18 @19 freqcode $1. ICN $ 20-40;
run;

 

Alternatively you may change the INPUT statemnet:

input @1 clamno $13. @15. lineno  $1. @19 $1.  @27 icn $13. ;

 

Is lineno just one digit? can it be longer?  if yes - adapt its length to the maximum available digits ($n. instead $1.)

I freqcode just one digit ? - if need adapt length to maximum digits.

athapa1183
Obsidian | Level 7

Looks fine and works.

Sry I didn't have SAS at home so replying now.

Thank you So much Shmuel.

Kurt_Bremser
Super User

Do a proc contents on your dataset. This will reveal data types and assigned formats for your variables.

 

To post your SAS data in a readable and usable form here, use the hints from https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

The data step created might even give you enough hints about the nature of your data.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 1335 views
  • 0 likes
  • 4 in conversation