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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 678 views
  • 0 likes
  • 4 in conversation