BookmarkSubscribeRSS Feed
ChiragJPatel
Calcite | Level 5
I have typed datalines and run statement. I want to create a data set where I have 4 variables
1) character variable - Name with 8 length - which has values -  "File A, File B, File C, File D, File E"
2)numeric variables - Test1, Test2, Test3 which has values 10 20 30 and so on.
 
how can use # (line pointer) properly to create dataset. ? or anyother better way?
because 5th line has value for name , then line 6,7,8 for TEST and then again line 9th for Name.
 
datalines;
File A
10 20 30
40 50 60
70 80 90
File B
10 20 30
40 50 60
70 80 90
File C
10 20 30
40 50 60
70 80 90
File D
10 20 30
40 50 60
70 80 90
File E
10 20 30
40 50 60
70 80 90
run;
3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @ChiragJPatel  Are you after this?

 

data read;
infile cards truncover;
 input Name & $ ;
 do _n_=1 to 3;
  input test1-test3;
  output;
 end;
cards;
File A
10 20 30
40 50 60
70 80 90
File B
10 20 30
40 50 60
70 80 90
File C
10 20 30
40 50 60
70 80 90
File D
10 20 30
40 50 60
70 80 90
File E
10 20 30
40 50 60
70 80 90
;
run;
Astounding
PROC Star

Are you concerned with leaning to read DATALINES, or are you concerned with creating the data set?  If you just want to create the data set, you could skip the DATALINES entirely:

 

data want;
   length name $ 8;
   do name = 'File A', 'File B', 'File C', 'File D', 'File E';
      do Test1 = 10, 40, 70;
         Test2 = Test1 + 10;
         Test3 = Test1 + 20;
         output;
      end;
   end;
run;
Patrick
Opal | Level 21

@ChiragJPatel 

It's rather rare that you want to address multiple source rows in a single iteration of the data step. The reason is: If there is any variation in the number of source rows per "block" then everything gets messed-up.

If dealing with a case like yours you normally try to implement some logic which determines what source of row you're dealing with and then fork to the logic required to read this type of data. That's what below code does.

 

data read;
  infile datalines truncover dlm=' ';
  retain name;
  /* read source row into input buffer. @ to keep pointer on this row */
  input @;
  /* if first character in input buffer is not a digit then read as name */
  if anydigit(substrn(_infile_,1,1))=0 then
    input name $8.;
  /* else: read numbers. Only write to output if we're reading numbers */
  else
    do;
      input var1 var2 var3;
      output;
    end;
  datalines;
File A
10 20 30
40 50 60
70 80 90
File B
10 20 30
40 50 60
70 80 90
File C
10 20 30
40 50 60
70 80 90
File D
10 20 30
40 50 60
70 80 90
File E
10 20 30
40 50 60
70 80 90
;

 

Here how the code would need to look like if reading from multiple lines.

data read;
  infile datalines truncover dlm=' ' n=4;
  retain name;
  input 
    #1 name $8. 
    #2 var1 var2 var3 @;
  output;
  input #3 var1-var3 @;
  output;
  input #4 var1-var3;
  output;
  datalines;
File A
10 20 30
40 50 60
70 80 90
File B
10 20 30
40 50 60
70 80 90
File C
10 20 30
40 50 60
70 80 90
File D
10 20 30
40 50 60
70 80 90
File E
10 20 30
40 50 60
70 80 90
;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 683 views
  • 0 likes
  • 4 in conversation