SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BlayLay
Obsidian | Level 7

I have a csv file (that's not really a true csv file) that I'm trying to import and am wondering if there is a way to only import x number of rows until it reads the "total" line. The reason being, my PROC IMPORT is erroring out when it hits the "Total" value, as it's not a date value (which makes sense). The number of rows varies on a weekly basis, so I can't set it based on x number of rows. However, I'm hoping I can set a condition in the import statement to only read in the data until it get's to the "total" line and then ignore anything after the last date. I'll provide an example table below: 

 

DateMakeCOUNT
JAN2019TOYOTA3
FEB2019FORD17
MAR2019CHEVY5
APR2019MAZDA5
TOTAL 30
 RANGE JAN2019 TO APR2019 

 

I'm not sure if a WHERE statement could work, but am open to any and all suggestions.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Pseudo code of similar process:

data read;
   infile "<path>\somefile.csv" dlm=',' dsd lrecl=32000;
<here should go informat settings for the variables you want
you should be able to get them from one your prod IMPORT
steps as the proc writes data step code to the log
that it generates> input @; if _infile_ =: "Total" then do; <do nothing>
input; end; else do;
input date make count;
end;
; run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

A) do NOT use PROC IMPORT

B) in the DATA step, first do a "blind read" (input@;), then check if the word "TOTAL" appears in _infile_, and if not proceed to read from @1.

 

If that is not clear enough, please post your data step code, and I will show you the necessary modification.

Amir
PROC Star

Hi @BlayLay ,

 

Instead of using proc import, you could try using a data step so that you could examine the text in your input file by reading the first column as character and if it is a date then apply an informat to convert it to a SAS date.

 

 

There are some examples of reading a csv in:
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lestmtsref&docsetTarget=n...

 

Kind regards,

Amir.

ballardw
Super User

Pseudo code of similar process:

data read;
   infile "<path>\somefile.csv" dlm=',' dsd lrecl=32000;
<here should go informat settings for the variables you want
you should be able to get them from one your prod IMPORT
steps as the proc writes data step code to the log
that it generates> input @; if _infile_ =: "Total" then do; <do nothing>
input; end; else do;
input date make count;
end;
; run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1519 views
  • 1 like
  • 4 in conversation