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!
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;
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;
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;
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.