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;
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;
Hi @christ0fr and welcome to the SAS Support Communities!
You just forgot the parentheses:
input @1 (Q1-Q7) (1.)
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;
Thanks so much Reeza!!
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;
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: