BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
harmonic
Obsidian | Level 7

Hello community,

 

I wanted to ask you if there is a way to read the rows from a csv file if starts with a specific string.

 

For example in this csv I want just the rows that starts with "2021-01", so the row that starts with 2021-09 will be deleted.

fabiopuddu_0-1715005620005.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Like this:

data _null_;
  file "R:\some_test_file.csv";
  put "2021-01-01,1,2,3";
  put "2021-01-02,4,5,6";
  put "2021-01-03,7,8,9";

  put "2021-09-01,1,2,3";
  put "2021-09-02,4,5,6";
  put "2021-09-03,7,8,9";
run;


filename f "R:\some_test_file.csv";
data want;
  infile f dlm=",";
  input @;
  if "2021-01" =: _infile_;
  input date yymmdd10. a b c;
  format date date11.;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Like this:

data _null_;
  file "R:\some_test_file.csv";
  put "2021-01-01,1,2,3";
  put "2021-01-02,4,5,6";
  put "2021-01-03,7,8,9";

  put "2021-09-01,1,2,3";
  put "2021-09-02,4,5,6";
  put "2021-09-03,7,8,9";
run;


filename f "R:\some_test_file.csv";
data want;
  infile f dlm=",";
  input @;
  if "2021-01" =: _infile_;
  input date yymmdd10. a b c;
  format date date11.;
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

Please provide sample data as text and not as screenshot - ideally via a SAS data step that creates the sample data.

data demo;
  infile datalines4 truncover dsd dlm=';';
  input row_dt :yymmdd10. @;
  if put(row_dt,yymmn6.)='202101';
  input val :best32. next_var :$10.;
  format row_dt date9.;
datalines4;
2021-01-18;111;keep
2021-09-01;34;drop
2021-01-22;111;keep
.;111;keep
;;;;

proc print data=demo;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 2 replies
  • 729 views
  • 2 likes
  • 3 in conversation