DATA Step, Macro, Functions and more

readin raw data repeated block with an id

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

readin raw data repeated block with an id

[ Edited ]

Hi,

 

 

I would like to import raw data that has multiple observation in a single record. each records start with a identifyer variable.  the following example show the data with the sas code i am using. It works fine :

 

raw data :

1824 1,323.34 2,472.85
1943 1,908.34
2046 1,423.52 1,673.46 3,276.65
2063 2,345.34 2,452.45 3,523.52 2,983.01


data excedata;
infile mydat(excedata.txt)  missover;
input id sales : comma. @ ;
 do while (sales ne .);
   output;
   input sales : comma. @;
end;
run;

 

Howver  when I want to use the same coding strucutre for this data I got invalid message in my log

 

182-04-7814 09FEB99 530 04MAY99 610 
302-57-5023 09FEB99 480 04MAY99 530 
123-89-8470 09FEB99 560 04MAY99 570 13JUL99 610 
152-64-0014 09FEB99 780 
813-63-7456 09FEB99 790  



data satdat2;
infile mydat(satdata2.txt) missover ;
input ssn $11. @13 date date7. @21 code 3. @ ;
do while (code ne .);
output;
input date date7. code 3. @ ;
end;
run;



it says invalide data for code.  Any clue ?

 

Cheers

 

Skp


Accepted Solutions
Solution
‎03-24-2016 02:28 PM
Super User
Posts: 9,662

Re: readin raw data repeated block with an id

data have;
infile '/folders/myfolders/test.txt' truncover;
input ssn : $11.  date : date7.  code @ ;  
do while (code ne .); 
 output;
 input date : date7.  code @ ;
end;
format date  date7.;
run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: readin raw data repeated block with an id

Notice that in your first program you have INPUT SALES : comma. @;  the colon : is the key to using an INFORMAT in an input statement with LIST input. You could alternatively use an INFORMAT statement to associate the INFORMATS and just use variables names in the input statement.

informat sales comma.;
input sales @;

 

Super User
Posts: 10,466

Re: readin raw data repeated block with an id

Generally it helps to post the log with code and error messages.

Note that the first part of your input has read @ specific columns. But the loop doesn't do that. So with the formats on the input it forces reading at the columns the previous field ended. So you end up with the code trying to read the last character of the date a space and the first character of the code. Since the code is numeric an imbedded space is invalid to read. You can avoid that by @data_null__'s suggestion of adding informat statements and leaving the formats off the input statements tranforming the code to list input OR add appropriate +n to shift the start column over to the start of the variable.

Solution
‎03-24-2016 02:28 PM
Super User
Posts: 9,662

Re: readin raw data repeated block with an id

data have;
infile '/folders/myfolders/test.txt' truncover;
input ssn : $11.  date : date7.  code @ ;  
do while (code ne .); 
 output;
 input date : date7.  code @ ;
end;
format date  date7.;
run;

Contributor
Posts: 54

Re: readin raw data repeated block with an id

Thanks Ksharp !

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 298 views
  • 2 likes
  • 4 in conversation