SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dustychair
Pyrite | Level 9

Hi all,

I have a data set as in the picture (I'm not copying the data because I want you to see that I have missing values).The data has 108 items' scores (one digit) and then id1 (8 digits) (no space between items and id) and then one space and id2 (3 digits). The spaces you see are missing values not tab or any type of delimiter for items and id1. When I use infile statement all items are in one column. Could you help me to read this data?The txt file in the attachment has the data.

 

Thank you,

 

data.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @dustychair 

 

To read fixed-column input like this, you can take advantage of using the @column - notation in the Input statement. This gives you full control over the cursor position:

 

data test(drop=i);
	infile "c:\temp\data.txt";
	array score s1-s108;
	do i = 1 to 108;
		input @i score{i} 1. @;
	end;
	input @109 id1 8. @119 id2 $3.;
run;

 

By the way, there are 2 spaces between ID1 and ID2 in your data. The code above reflects that.

View solution in original post

5 REPLIES 5
anushreebiotech
Obsidian | Level 7

Hi,

 

Can you please put headers in your text file. Its difficult to understand like this.

 

Regards,

Anushree

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @dustychair 

 

To read fixed-column input like this, you can take advantage of using the @column - notation in the Input statement. This gives you full control over the cursor position:

 

data test(drop=i);
	infile "c:\temp\data.txt";
	array score s1-s108;
	do i = 1 to 108;
		input @i score{i} 1. @;
	end;
	input @109 id1 8. @119 id2 $3.;
run;

 

By the way, there are 2 spaces between ID1 and ID2 in your data. The code above reflects that.

Astounding
PROC Star

A good solution,  Also note that you can read sequentially without worrying about the starting position:

 

data test(drop=i);
	infile "c:\temp\data.txt";
	array score s1-s108;
	do i = 1 to 108;
		input score{i} 1. @;
	end;
	input id1 8. @119 id2 $3.;
run;

 

dustychair
Pyrite | Level 9
Thank you SO much!
Tom
Super User Tom
Super User

The data has 108 items' scores (one digit) and then id1 (8 digits) (no space between items and id) and then one space and id2 (3 digits).

Just translate your description into an INPUT statement. Use formatted input instead of list mode.

input (score1-score108) (1.) id1 $8. +1 id2 $3. ;

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 4563 views
  • 2 likes
  • 5 in conversation