data ds ;
infile datalines ;
input name$ 15;
cards;
abc_11234567898
abc_112345678989
;
run;
in above data I have 15 characters length observation only only , above 15 character length I don't. please anyone help me how to remove above 15 char length variable.
The LENGTH function will measure the length of variables.
The DELETE statement will delete observations.
In combination:
data ds ;
infile datalines ;
length name $ 16;
input name;
if length(name) ne 15 then delete;
cards;
abc_11234567898
abc_112345678989
;
DO NOT set the length of NAME to 15, or else you can't detect those rows that originally were longer than 15.
A few points ...
Evidently you made an error posting the question. The INPUT statement is missing a dot:
input name$ 15.;
not
input name$ 15;
If you want more than 15 characters, you need to define a variable that is more than 15 characters long. One way:
data ds ;
infile datalines ;
length name $ 16;
input name;
cards;
abc_11234567898
abc_112345678989
;
In the LENGTH statement, choose whatever length you would like for your variable. It doesn't have to be 16.
Notice that once the variable's length is already defined by the LENGTH statement, you can simplify the INPUT statement.
Also notice that you don't need a RUN; statement. Since the CARDS section can only appear at the end of a DATA step, the presence of a CARDS statement triggers SAS to run the DATA step (whether or not you have a RUN statement).
The LENGTH function will measure the length of variables.
The DELETE statement will delete observations.
In combination:
data ds ;
infile datalines ;
length name $ 16;
input name;
if length(name) ne 15 then delete;
cards;
abc_11234567898
abc_112345678989
;
DO NOT set the length of NAME to 15, or else you can't detect those rows that originally were longer than 15.
If the goal is to check how long the string was on the line that is being read then testing the length of what ended up in the variable is the wrong test.
Instead you could test how many characters before the last non-blank character there were in the line by testing the _INFILE_ automatic variable.
input string $15.;
if length(_infile_) > 15 then delete;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.