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 datelines;

input num;

cards;

1234567891

12345678901

run;

 

in above data I have observation length 10  but when  I am use if condition not work .

 

like ex:

data ds2 ds3;

set ds;

if length(num) eq 10 then output ds2;

else output ds3;

run;

 

above data  if  condition not work anyone can explain me , above data by default length take best12. 

how to find exact length .

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

Since the num variable in the ds data set is a numeric variable, the length of a string cannot be obtained.
If it is treated as a character variable, the length can be obtained as follows.

 

data ds;
length num $20;
input num $;
cards;
1234567891
12345678901
run;

data _null_;
  set ds;
  len=length(num);
  put len=;
run;

 

Or, if you want to treat them as numbers, you can convert them to charactors.

data ds;
input num;
cards;
1234567891
12345678901
run;

data _null_;
  set ds;
  len=length(strip(put(num,best12.)));
  put len=;
run;

 

 

 

View solution in original post

4 REPLIES 4
japelin
Rhodochrosite | Level 12

Since the num variable in the ds data set is a numeric variable, the length of a string cannot be obtained.
If it is treated as a character variable, the length can be obtained as follows.

 

data ds;
length num $20;
input num $;
cards;
1234567891
12345678901
run;

data _null_;
  set ds;
  len=length(num);
  put len=;
run;

 

Or, if you want to treat them as numbers, you can convert them to charactors.

data ds;
input num;
cards;
1234567891
12345678901
run;

data _null_;
  set ds;
  len=length(strip(put(num,best12.)));
  put len=;
run;

 

 

 

Patrick
Opal | Level 21

Your variable NUM is of type NUMERIC. 

Length() is a string function that requires variables of type CHARACTER. 

 

If you want to know for a numerical variable how many digits an integer has then below one way to get there.

data test;
  set ds;
  l=length(put(int(num),f16. -l));
run;

 

venunaidu
Fluorite | Level 6

Thank u so much for helping 

Ksharp
Super User
data ds2 ds3;
set ds; 
if int(log10(num))+1 eq 10 then output ds2;
else output ds3;
run;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 601 views
  • 0 likes
  • 4 in conversation