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

Hi,

 

I'm trying to run some very simple code to read a file (attached). I expect 7 variables Q1 through 7 each with one one-digit number; however, the code below gives me a 7 digit number in Q1, the age in Q2, the IQ in Q3. I tried putting spaces between the Q1-Q7 data in the dat file, but then I get every variable with only one digit (i.e., AGE will be one digit with the second digit being placed in IQ). I'm a new SAS user working my way through exercises and this was one that I couldn't make work (even using a datalines statement instead of infile).

 

Thanks

 

 

data D1;
	infile 'C:/Users/myname/Desktop/volunteer.txt';
	input	#1	@1	Q1-Q7	1.
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Put brackets around the Q and format. I don't think you need the #1 either. 

 

data D1;
	input   @1	(Q1-Q7)	(1.)
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
cards;
2234243 22  98  1 M
3424325 20 105  2 M
3242424 32  90  3 F
3242323  9 119  4 F
3232143  8 101  5 F
3242242 24 104  6 M
;;;;

RUN;

@christ0fr wrote:

Hi,

 

I'm trying to run some very simple code to read a file (attached). I expect 7 variables Q1 through 7 each with one one-digit number; however, the code below gives me a 7 digit number in Q1, the age in Q2, the IQ in Q3. I tried putting spaces between the Q1-Q7 data in the dat file, but then I get every variable with only one digit (i.e., AGE will be one digit with the second digit being placed in IQ). I'm a new SAS user working my way through exercises and this was one that I couldn't make work (even using a datalines statement instead of infile).

 

Thanks

 

 

data D1;
	infile 'C:/Users/myname/Desktop/volunteer.txt';
	input	#1	@1	Q1-Q7	1.
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
RUN;

 


 

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @christ0fr and welcome to the SAS Support Communities!

 

You just forgot the parentheses:

input @1 (Q1-Q7) (1.)
christ0fr
Calcite | Level 5
Thanks FreelanceReinhard!
Reeza
Super User

Put brackets around the Q and format. I don't think you need the #1 either. 

 

data D1;
	input   @1	(Q1-Q7)	(1.)
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
cards;
2234243 22  98  1 M
3424325 20 105  2 M
3242424 32  90  3 F
3242323  9 119  4 F
3232143  8 101  5 F
3242242 24 104  6 M
;;;;

RUN;

@christ0fr wrote:

Hi,

 

I'm trying to run some very simple code to read a file (attached). I expect 7 variables Q1 through 7 each with one one-digit number; however, the code below gives me a 7 digit number in Q1, the age in Q2, the IQ in Q3. I tried putting spaces between the Q1-Q7 data in the dat file, but then I get every variable with only one digit (i.e., AGE will be one digit with the second digit being placed in IQ). I'm a new SAS user working my way through exercises and this was one that I couldn't make work (even using a datalines statement instead of infile).

 

Thanks

 

 

data D1;
	infile 'C:/Users/myname/Desktop/volunteer.txt';
	input	#1	@1	Q1-Q7	1.
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
RUN;

 


 

christ0fr
Calcite | Level 5

Thanks so much Reeza!!

novinosrin
Tourmaline | Level 20

Array and input

 

data D1;
array Q{7};
	input    Q{*} 1.
			@9	AGE	2.
			@12	IQ	3.
			@16	NUMBER	2.
			@19	SEX    $1. ;
cards;
2234243 22  98  1 M
3424325 20 105  2 M
3242424 32  90  3 F
3242323  9 119  4 F
3232143  8 101  5 F
3242242 24 104  6 M
;;;;

RUN;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1170 views
  • 3 likes
  • 4 in conversation