SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
venunaidu
Fluorite | Level 6

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.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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).

venunaidu
Fluorite | Level 6
above data i have 15 char observation only output ,remaining more than 15
char or below 15 char should be deleted please help me on that .

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 4 replies
  • 1182 views
  • 0 likes
  • 3 in conversation