DATA Step, Macro, Functions and more

Input cvs file with infile and @ n pointer to format variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 142
Accepted Solution

Input cvs file with infile and @ n pointer to format variables

Any idea where I'm making mistake here?

See first obs of data attached to this post.

 

DATA want;
  infile 'C:\Users\Maggie\Desktop\data' delimiter=',';
  input @1 time 1.
        @3 group 1.
		@5 subj 4.
		@10(GH1 PF01-PF10 RP1-RP4 RE1-RE3 SF1
             BP1-BP2 VT1 MH1 MH2 MH3 VT2 MH4 VT3 MH5
             VT4 SF2 GH2 GH3 GH4 GH5) (1.); 
run;

Any hints are appreciated. 

Thanks


Accepted Solutions
Solution
‎05-24-2017 12:41 PM
Super User
Posts: 7,863

Re: Input cvs file with infile and @ n pointer to format variables

You do not need positions when you have a delimited file, SAS finds the columns through the delimiters on its own.

Since all variables are numbers, also drop the informats from the input.

And also add firstobs=2 to the infile statement so you skip the header line.

 

When reading a delimited file with formats, add a colon before the format to prevent pure formatted input that disregards the delimiters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: Input cvs file with infile and @ n pointer to format variables

What about:

DATA want;
  infile 'c:\temp\data.csv' delimiter=',' dsd truncover firstobs=2;
  input 
    ( 
      Time Group Subj GH1 PF01 PF02 PF03 PF04 PF05 PF06 PF07 PF08 
      PF09 PF10 RP1 RP2 RP3 RP4 RE1 RE2 RE3 SF1 BP1 BP2 VT1 MH1 MH2 
      MH3 VT2 MH4 VT3 MH5 VT4 SF2 GH2 GH3 GH4 GH5 
    )
    (:best32.)
  ;
run;
Frequent Contributor
Posts: 142

Re: Input cvs file with infile and @ n pointer to format variables

@PatrickIt worked out except resulting missing in variable "subj"? strange?

error.png

Respected Advisor
Posts: 4,173

Re: Input cvs file with infile and @ n pointer to format variables

@SUNY_Maggie

You're dealing with a simple comma separated list. Use list input and do not use any pointer control (the @) as this messes up things for you. 

http://support.sas.com/documentation/cdl/en/lestmtsref/69738/HTML/default/viewer.htm#n0lrz3gb7m9e4rn...

 

Look into: Example 4: Reading Comma-Delimited Data with List Input and an Informat

 

BTW: If you would start using SAS Studio or SAS EG then you could take advantage of import wizards which help you to generate the correct SAS code. 

Contributor
Posts: 21

Re: Input cvs file with infile and @ n pointer to format variables

 infile 'C:\Users\Maggie\Desktop\data.csv' 

You dint add the file extension  

Solution
‎05-24-2017 12:41 PM
Super User
Posts: 7,863

Re: Input cvs file with infile and @ n pointer to format variables

You do not need positions when you have a delimited file, SAS finds the columns through the delimiters on its own.

Since all variables are numbers, also drop the informats from the input.

And also add firstobs=2 to the infile statement so you skip the header line.

 

When reading a delimited file with formats, add a colon before the format to prevent pure formatted input that disregards the delimiters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,076

Re: Input cvs file with infile and @ n pointer to format variables

[ Edited ]

If you have a delimited file then use the DSD option and do not use column pointers or formatted input.

Your file is simple since all of the variables appear to be numbers.

data want;
  infile "&path/data.csv" dsd dlm=',' firstobs=2 truncover ;
  input Time Group Subj  GH1 PF01-PF10 RP1-RP4 
        RE1-RE3 SF1 BP1 BP2 VT1 MH1-MH3 VT2 MH4 VT3 MH5 VT4 SF2 GH2-GH5
  ;
run;

If your file is more complex, for example with some character variables then you would want to first define the variables using a LENGTH statement before the INPUT statement. 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 180 views
  • 0 likes
  • 5 in conversation