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

Hello,

 

Sorry I'm a beginner to sas I was hoping you can help me. I was trying to create a data set with the following columns:

name

sport

gender

weight

height

 

from this txt file format:

Eugene,Basketball
M_52.50_120.33
Vincent,Football
M_43.25_115.15
Chichi,Football
F_35.75_090.50
Videl,Football
F_32.65_085.50
Hanamichi,Basketball
M_69.15_145.55
Alfred,Basketball
M_52.25_135.25
Dennis,Basketball
M_55.15_123.45
Alice,Softball
F_35.30_070.50
Michael,Softball
M_72.43_111.12
Rodrigo,Softball
M_66.15_075.92
Ara,Football
F_92.88_089.99
Karla,Football
F_52.13_078.98

 

I was able to easily do the name and sport but having trouble with gender/weight/height since they are in the next row in the txt file. 

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

The following code makes use of the INFILE and INPUT (list) statements and the INFILE DELIMITER option  (DLM).
There are good examples in the documentation for further reading

Thanks

 

/*
	Create sample data
*/

data _null_ ;
	file "have.txt" ;
	infile cards ;
	input ;
	put _infile_ ;
cards ;
Eugene,Basketball
M_52.50_120.33
Vincent,Football
M_43.25_115.15
Chichi,Football
F_35.75_090.50
Videl,Football
F_32.65_085.50
Hanamichi,Basketball
M_69.15_145.55
Alfred,Basketball
M_52.25_135.25
Dennis,Basketball
M_55.15_123.45
Alice,Softball
F_35.30_070.50
Michael,Softball
M_72.43_111.12
Rodrigo,Softball
M_66.15_075.92
Ara,Football
F_92.88_089.99
Karla,Football
F_52.13_078.98
;
run ;

/*
	Read sample data into SAS Dataset
*/

data work.want ;
	
	infile "have.txt" dlm=",_";    /* Point to our source data, set the delimiter to comma and underscore */
	input name $ sport $ ;         /* Read the first line */
	input gender $ weight height ; /* Read the second line */
	output work.want ; 			   /* output to SAS dataset */
run ;

 

View solution in original post

1 REPLY 1
AMSAS
SAS Super FREQ

The following code makes use of the INFILE and INPUT (list) statements and the INFILE DELIMITER option  (DLM).
There are good examples in the documentation for further reading

Thanks

 

/*
	Create sample data
*/

data _null_ ;
	file "have.txt" ;
	infile cards ;
	input ;
	put _infile_ ;
cards ;
Eugene,Basketball
M_52.50_120.33
Vincent,Football
M_43.25_115.15
Chichi,Football
F_35.75_090.50
Videl,Football
F_32.65_085.50
Hanamichi,Basketball
M_69.15_145.55
Alfred,Basketball
M_52.25_135.25
Dennis,Basketball
M_55.15_123.45
Alice,Softball
F_35.30_070.50
Michael,Softball
M_72.43_111.12
Rodrigo,Softball
M_66.15_075.92
Ara,Football
F_92.88_089.99
Karla,Football
F_52.13_078.98
;
run ;

/*
	Read sample data into SAS Dataset
*/

data work.want ;
	
	infile "have.txt" dlm=",_";    /* Point to our source data, set the delimiter to comma and underscore */
	input name $ sport $ ;         /* Read the first line */
	input gender $ weight height ; /* Read the second line */
	output work.want ; 			   /* output to SAS dataset */
run ;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 468 views
  • 1 like
  • 2 in conversation