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

I have a csv data which i am importing in sas , the first column of which has ',' only, what should i use to read in as the values are transcending to next column, i have used dsd,dlm,missover.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Please copy/paste the first few lines of the csv file into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

 

And post the code you tried into a window opened with the "little running man" button right next to the one indicated.

View solution in original post

12 REPLIES 12
Kurt_Bremser
Super User

Please copy/paste the first few lines of the csv file into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

 

And post the code you tried into a window opened with the "little running man" button right next to the one indicated.

archita
Fluorite | Level 6

ID NAME STATE WEIGHT AGE
, SAM WA 67 12
, TOM TX 90 15
, GINNY MI 89 16

Kurt_Bremser
Super User

See this:

data want;
infile datalines firstobs=2 dlm=" " dsd truncover;
input id $ name $ state $ weight age;
drop id;
/* or this */
if id = "," then id = " ";
datalines;
ID NAME STATE WEIGHT AGE
, SAM WA 67 12
, TOM TX 90 15
, GINNY MI 89 16
;

Replace the DATALINES keyword in the INFILE statement with your path to the file, and replace the DATALINES block with a RUN statement.

archita
Fluorite | Level 6

I am not getting the desired output

archita
Fluorite | Level 6

Id,NAME,STATe,WEIGHT,AGE
,WA,67,12
,TOM,TX,90,15
,GINNY,MI,89,16

 

this is how the data looks ,i want to read in this into sas.

Kurt_Bremser
Super User

Open your file with a text editor, and copy/paste the content into a code box, as described earlier. Only when I can be half-way sure I see what you really have, will I work on further code.

archita
Fluorite | Level 6

please find the attached file.

Kurt_Bremser
Super User
data want;
infile '/folders/myfolders/test1.txt' firstobs=2 dlm="," dsd truncover;
input id $ name $ state $ weight age;
run;

proc print data=want;
run;

Result:

Beob.	id	name	state	weight	age
1	 	WA	67	12	.
2	 	TOM	TX	90	15
3	 	GINNY	MI	89	16

The first data line misses a name.

archita
Fluorite | Level 6
Thank you it is working now.
archita
Fluorite | Level 6
ID NAME STATE WEIGHT AGE
"," SAM WA 67 12
"," TOM TX 90 15
"," GINNY MI 89 16
Tom
Super User Tom
Super User

@archita wrote:
ID NAME STATE WEIGHT AGE
"," SAM WA 67 12
"," TOM TX 90 15
"," GINNY MI 89 16

Why did you post this? It does not look anything at all like the file your attached before.

Here is what those lines look like. Notice how I used the Insert Code button on the forum editor to allow me to paste in the text and have it displayed without being interpretted as paragraphs.

Id,NAME,STATe,WEIGHT,AGE
,WA,67,12
,TOM,TX,90,15
,GINNY,MI,89,16

If you don't want to keep the ID variable in your SAS dataset you can use a DROP statement in your data step that reads the text file.

data want;
  infile 'myfile.txt' dsd firstobs=2 truncover;
  input Id $ NAME $ STATe $ WEIGHT AGE;
  drop id;
run;
archita
Fluorite | Level 6
thank you it is working .

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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