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 ;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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