How do I read in a .txt file with double quotes without splitting up multi-word names?

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

How do I read in a .txt file with double quotes without splitting up multi-word names?

 

Hello, 

 

I've been trying to read this dataset into SAS Studio (Release: 3.71 - Basic Edition). As shown below, everything is in double quotes:  

 

"log.radon" "floor" "uranium" "county" "county.name"
"1" "0.78845736036427" "1" "-0.6890475953545" "1" "AITKIN "
"2" "0.78845736036427" "0" "-0.6890475953545" "1" "AITKIN "
"3" "1.06471073699243" "0" "-0.6890475953545" "1" "AITKIN "

(more lines of data here)

"917" "1.6094379124341" "0" "-0.0900242748478867" "84" "WRIGHT "
"918" "1.30833281965018" "0" "0.355286981163884" "85" "YELLOW MEDICINE "
"919" "1.06471073699243" "0" "0.355286981163884" "85" "YELLOW MEDICINE "

 

 

 

data temp;
length ID Radon Floor Uranium County countyname $ 40;
infile "&path/radon.MN.txt" dlm='" "' firstobs=2;
input ID Radon Floor Uranium County countyname$;
run;

All of my numeric variables read in perfectly, but my one character variable will read in only the first word. 

For example, obs 919's county.name is "YELLOW MEDICINE" but I the output just says YELLOW.  

 

 

SAS help.JPG

 

Because SAS stops after it reaches the first space between words and moves the next word to a new variable, I am guessing it's a problem with how I'm setting up dlm.  How can I prevent this miscommunication with SAS so that it reads in everything between double quotes and not just the first word?

 
Note: I've tried dsd but the error message seems to say that won't work with this file.  
"ERROR: Quotes (' or ") have been detected in the delimiter (DLM=)
string. Quotes cannot be used as delimiter characters when the
DSD option is also specified."

 

Also, I apologize for anything in this post that I've formatted poorly or vaguely. I appreciate any formatting feedback, too.  

 

Thank you for any thoughts or suggestions. 

Nicole


Accepted Solutions
Solution
‎04-18-2018 02:32 AM
Super User
Posts: 10,258

Re: How do I read in a .txt file with double quotes without splitting up multi-word names?

Posted in reply to nzelinsky

Try this:

data temp;
 length ID Radon Floor Uranium County countyname $ 40;
 infile "&path/radon.MN.txt" dlm=' ' dsd firstobs=2; 
 input ID Radon Floor Uranium County countyname$;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎04-18-2018 02:32 AM
Super User
Posts: 10,258

Re: How do I read in a .txt file with double quotes without splitting up multi-word names?

Posted in reply to nzelinsky

Try this:

data temp;
 length ID Radon Floor Uranium County countyname $ 40;
 infile "&path/radon.MN.txt" dlm=' ' dsd firstobs=2; 
 input ID Radon Floor Uranium County countyname$;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,258

Re: How do I read in a .txt file with double quotes without splitting up multi-word names?

Posted in reply to nzelinsky

And here a more specific version, with my ideas for variable types/lengths:

data want;
infile cards dlm=' ' dsd firstobs=2;
length
  ID $3
  Radon 8
  Floor 3
  Uranium 8
  County $2
  countyname $ 40
;
input ID Radon Floor Uranium County countyname;
cards;
"log.radon" "floor" "uranium" "county" "county.name"
"1" "0.78845736036427" "1" "-0.6890475953545" "1" "AITKIN "
"2" "0.78845736036427" "0" "-0.6890475953545" "1" "AITKIN "
"3" "1.06471073699243" "0" "-0.6890475953545" "1" "AITKIN "
"917" "1.6094379124341" "0" "-0.0900242748478867" "84" "WRIGHT "
"918" "1.30833281965018" "0" "0.355286981163884" "85" "YELLOW MEDICINE "
"919" "1.06471073699243" "0" "0.355286981163884" "85" "YELLOW MEDICINE "
;
run;

Note that your initial code would read all variables as $40.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 149 views
  • 1 like
  • 2 in conversation