2
votes

I have a dataset having 20 observations and 6 variables ID, Gender, Age,Height,Weight,Year. All are numeric except gender variable. I would like to extract 10 observations starting from fifth observation using SAS macros.

I have the code below to import and extract the selected rows from the table.

I want to extract the selected rows using macros as part of an exercise. Please let me know your advice how to use macros to extract specific observations. Thank you for your time.

%macro one (a, b, c);
    proc import out=&a
    datafile= "C:\Users\komal\Desktop\&b"
    dbms=&c replace;
    getnames=yes;
    run;
%mend one;

%one (outcsv, Sample.csv, csv);

data test;
    set outcsv;
    if _N_ in (5,6,7,8,9,10,11,12,13,14) then output;
    run;
2
Is your source actually a text file? Like a CSV or other delimited file?Tom
It is csv file.K Bh

2 Answers

2
votes

you could do something like this

 %macro one (a, b, c,strtpt,endpt);
    proc import out=&a
    datafile= "C:\Users\komal\Desktop\&b"
    dbms=&c replace;
   getnames=yes;
   run;
  data test;
   set &a;
    if _n_ >= &strtpt and _n_ =< &endpt;
 run;
 %mend one;

%one (outcsv, Sample.csv, csv,5,14);
2
votes

There is no need to use PROC IMPORT to read from a CSV file. Especially if you already know the names/types of the variables. So something like this should work.

data want ;
  infile "C:\Users\komal\Desktop\&b" dsd firstobs=5 obs=14 truncover ;
  input ID Gender $ Age Height Weight Year ;
run;

You might need to use 6 to 15 instead if the file has a header row.