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 .
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;
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;
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;
Thank u so much for helping
data ds2 ds3; set ds; if int(log10(num))+1 eq 10 then output ds2; else output ds3; run;
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!
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.