BookmarkSubscribeRSS Feed
JackZ295
Pyrite | Level 9

Hi! Is there a way to read specific rows of a CSV file into SAS or to limit the number of rows being read from a CSV file into SAS? For example, if you had a data set with 1000 rows, but you just wanted to read in the first 800 rows, is there a way to do so? Any input regarding this would be much appreciated. Thanks so much! 

5 REPLIES 5
SASKiwi
PROC Star

Something like this should do it. Just adjust for the number of columns and the maximum length you want to read:

 

data temp (compress=yes);
   infile 'myfile.csv' dsd truncover firstobs=2 obs=800;
   length x1-x100 $200 ;
   input x1-x100;
run;

 

 

JackZ295
Pyrite | Level 9

Thanks so much! I was actually wondering if it was possible to do it in a proc import procedure. My code is currently as follows: 

 

options MSGLEVEL=I;

options validvarname=v7;

proc import out=one

datafile='/home/user/Assessment/test_1.csv'

DBMS=csv replace;

getnames=yes;

run;
SASKiwi
PROC Star

Unfortunately I don't think there is a PROC IMPORT option for this. However, if you add this before running PROC IMPORT you should see a DATA step in the SAS log that you can copy back into the SAS editor to add the OBS = option:

options source;

 

Patrick
Opal | Level 21

Proc Import generates and then executes a SAS data step (which you can see in the SAS log). 

You could set options obs=800 before proc import. This will lead for the generated data step to stop execution after row 800 is read.

You then set options obs=max; right after proc import.

RD2
Fluorite | Level 6 RD2
Fluorite | Level 6

Using Powershell answered by ChatGPT:  Import-Csv -Path "C:\path\to\csv\file.csv" | Select-Object -First 500 | Export-Csv -Path "C:\path\to\csv\first500.csv" -NoTypeInformation

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 2831 views
  • 2 likes
  • 4 in conversation