DATA Step, Macro, Functions and more

How do I apply an informat for variable record length

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How do I apply an informat for variable record length

[ Edited ]

Hi Guyz, I'm new to SAS programming. I'm writing an instream program in sas studio 9.4. The ename variable consists of variable length characters. I don't know which informat to use. When I'm running the program, SAS is throwing me an error message. Please someone help me out in writing the program in a correct way. 

 

data temp;
input Empid Ename $char16. Salary 6. Loc $char10. ; 
datalines; 
101 Vamshi Gulaigiri 60000 Hyderabad 
102 Aditya Vanipenta 70000 Bangalore 
103 Keerthi Perala 35000 Hyderabad 
104 Kesav Murari 80000 Newyork 
105 Gopala Krishna 50000 Hyderabad 
; 
run; 
proc print data = temp; 
run;

Error Message [log]: 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 55 56 data temp; 57 infile datalines; 58 input Empid Ename $char16. Salary 6. Loc $char10. ; 59 datalines; NOTE: Invalid data for Salary in line 62 22-27. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0 62 103 Keerthi Perala 35000 Hyderabad Empid=103 Ename=Keerthi Perala 3 Salary=. Loc=yderabad _ERROR_=1 _N_=3 NOTE: The data set WORK.TEMP has 5 observations and 4 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds user cpu time 0.01 seconds system cpu time 0.00 seconds memory 784.15k OS Memory 28580.00k Timestamp 03/21/2016 03:14:34 PM Step Count 70 Switch Count 50 Page Faults 0 Page Reclaims 377 Page Swaps 0 Voluntary Context Switches 146 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 264 63 ; 64 run; 65 proc print data = temp; 66 67 run; NOTE: There were 5 observations read from the data set WORK.TEMP. NOTE: PROCEDURE PRINT used (Total process time): real time 0.02 seconds user cpu time 0.03 seconds system cpu time 0.00 seconds memory 1649.87k OS Memory 28836.00k Timestamp 03/21/2016 03:14:34 PM Step Count 71 Switch Count 28 Page Faults 0 Page Reclaims 155 Page Swaps 0 Voluntary Context Switches 57 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 8 68 69 70 71 72 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 84

Accepted Solutions
Solution
‎03-28-2016 07:42 AM
Respected Advisor
Posts: 3,777

Re: How do I apply an informat for variable record length

I think all you need is to change

 

input Empid Ename $char16. Salary 6. Loc $char10. ; 

to this

input Empid Ename &$16. Salary :6. Loc :$10. ; 

You are trying to use formatted when you need list input.  Missing values in the data will need to be entered as a period and you need two spaces between ENAME and SALARY to read a character field with embedded space.

 

62 103 Keerthi Perala 35000 Hyderabad

View solution in original post


All Replies
Super User
Posts: 6,928

Re: How do I apply an informat for variable record length

[ Edited ]

First, some hints:

 

To post SAS code, use the "little running man" icon; this displays the code in your post with a fixed width font and typical SAS syntax highlighting.

To post log excerpts and the like, use the "{i}" icon, which also uses a fixed width font, but without syntax highlighting.

 

Regarding your problem:

Your "ename" column contains values with a delimiter inside. This makes it very hard to handle, if not impossible, with a simple input.

If you have access to the data source, try to wrap the column in quotes and use the DSD option in the infile statetement in SAS.

If you cannot manipulate the data source, options are:

- if ename always consists of the same number of words, read those separately and concatenate

- read the whole line into one variable, count the number of words, and then dynamically split the whole string into fields

 

ie

data temp;
length
  empid 8
  ename $30
  salary 8
  loc $10
  whole_line $ 100
;
input;
whole_line = _infile_;
x = countw(whole_line);
do i = 2 to x - 2;
  ename = catx(' ',trim(ename),trim(scan(whole_line,i)));
end;
empid = input(scan(whole_line,1),3.);
salary = input(scan(whole_line,x -1),best.);
loc = scan(whole_line,x);
drop whole_line x i;
cards;
101 Vamshi Gulaigiri 60000 Hyderabad
102 Aditya Vanipenta 70000 Bangalore
103 Keerthi Perala 35000 Hyderabad
104 Kesav Murari 80000 Newyork
105 Gopala Krishna 50000 Hyderabad
;
run;

proc print data = temp;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 4

Re: How do I apply an informat for variable record length

Thank you kurt for your answer. Actually it worked.

Super User
Posts: 17,784

Re: How do I apply an informat for variable record length

Try using an informat statement instead of placing informants in your input statement. 

 

Please post out your code within a code block, see the {I} or little paper notebook icon at the top of the editor. Your code/errors are not readable in their current format. 

Solution
‎03-28-2016 07:42 AM
Respected Advisor
Posts: 3,777

Re: How do I apply an informat for variable record length

I think all you need is to change

 

input Empid Ename $char16. Salary 6. Loc $char10. ; 

to this

input Empid Ename &$16. Salary :6. Loc :$10. ; 

You are trying to use formatted when you need list input.  Missing values in the data will need to be entered as a period and you need two spaces between ENAME and SALARY to read a character field with embedded space.

 

62 103 Keerthi Perala 35000 Hyderabad
New Contributor
Posts: 4

Re: How do I apply an informat for variable record length

Thank you for the answer. It worked and I understood the concept.

Super User
Posts: 17,784

Re: How do I apply an informat for variable record length

I've edited your post above to make it more clear.

 

I would use a different solution - assuming that the name is always in two parts, read it in as first and last name, so two separate variables. Then I use the concatenate function to create the name, to combine the first and last name into one variable.

I also create a specific informat statement to read the data.

 

data temp;
informat empid 8. first_name $20. last_name $20. Salary 8. loc $10.;
input Empid first_name last_name Salary Loc; 
datalines; 
101 Vamshi Gulaigiri 60000 Hyderabad 
102 Aditya Vanipenta 70000 Bangalore 
103 Keerthi Perala 35000 Hyderabad 
104 Kesav Murari 80000 Newyork 
105 Gopala Krishna 50000 Hyderabad 
; 
run; 
proc print data = temp; 
run;

 

New Contributor
Posts: 4

Re: How do I apply an informat for variable record length

Hi reeza, thank you for the answer.
☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 302 views
  • 3 likes
  • 4 in conversation