I have the following scenario, How do we get the character blank value when we have the missing numeric value? Why it's not working> Thanks for your input. I want avalc = '' when aval is missing.
data dd;
input aval;
cards;
2
.
;
run;
data ddx;
set dd;
AVALC = ifn (aval =.,'',left(put(aval, f2.)));
run;
Hello @SASuserlot,
It's a case for Maxim 2, "Read the log."
9 data ddx; 10 set dd; 11 AVALC = ifn (aval =.,'',left(put(aval, f2.))); 12 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 11:25 11:28 NOTE: There were 2 observations read from the data set WORK.DD.
Why did SAS convert a character value to numeric in each of the two observations? Because the IFN function returns a numeric result (which also makes AVALC a numeric variable as it hadn't been defined as character). So, switch to the IFC function, read the log again
14 data ddx; 15 set dd; 16 AVALC = ifc (aval =.,'',left(put(aval, f2.))); 17 run; INFO: Character variables have defaulted to a length of 200 at the places given by: (Line):(Column). Truncation can result. 16:1 AVALC NOTE: There were 2 observations read from the data set WORK.DD.
(hopefully, you are using options msglevel=I, otherwise you wouldn't get the important INFO)
and thus remember to insert the LENGTH statement before the assignment statement:
length AVALC $2;
Hint: left(put(aval, f2.)) could be replaced by put(aval, 2.-l).
Hello @SASuserlot,
It's a case for Maxim 2, "Read the log."
9 data ddx; 10 set dd; 11 AVALC = ifn (aval =.,'',left(put(aval, f2.))); 12 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 11:25 11:28 NOTE: There were 2 observations read from the data set WORK.DD.
Why did SAS convert a character value to numeric in each of the two observations? Because the IFN function returns a numeric result (which also makes AVALC a numeric variable as it hadn't been defined as character). So, switch to the IFC function, read the log again
14 data ddx; 15 set dd; 16 AVALC = ifc (aval =.,'',left(put(aval, f2.))); 17 run; INFO: Character variables have defaulted to a length of 200 at the places given by: (Line):(Column). Truncation can result. 16:1 AVALC NOTE: There were 2 observations read from the data set WORK.DD.
(hopefully, you are using options msglevel=I, otherwise you wouldn't get the important INFO)
and thus remember to insert the LENGTH statement before the assignment statement:
length AVALC $2;
Hint: left(put(aval, f2.)) could be replaced by put(aval, 2.-l).
Thank you, Not sure how I missed that log Note. It worked. Thank you
If all you want to do is change the APPEARANCE of a value, this is what formats do.
(See Maxim 8 "SAS provides a plethora of formats for input and output. Use them to your advantage. If one that fits your needs is not present, rolling your own will usually beat complicated data step logic.")
proc format;
value missf .=' ';
run;
data dd;
input aval;
format aval missf.;
cards;
2
.
;
In the special case of changing the appearance of a missing, its even simpler.
options missing=' ';
data dd;
input aval;
cards;
2
.
;
I gave the 'missing' option at the top, but not sure why it didn't work in my case( In my actual code, not the example here). But thanks for explaining how I can use formats in this scenario.
I have one question. This was the code written by an experienced guy ( retired in Data science) . I want to understand the result of the code in the following scenario.
Scenario:
'AVAL' is a numeric variable:
AVALC = left(put(aval, f2.));
1. What happens to AVALC if AVAL is missing and I given the 'MISSING' system option.
2.What happens to AVALC if AVAL is missing and I Don't given the 'MISSING' system option.
3. What happens to AVALC if AVAL is displaying as '.'
thank you for your time.
In your code, use "AVALC = ifn(aval = ., '', left(put(aval, f2.)));", it will assign a blank value when aval is missing.
@SethHunt wrote:
In your code, use "AVALC = ifn(aval = ., '', left(put(aval, f2.)));", it will assign a blank value when aval is missing.
No, it won't. By using IFN instead of IFC, you create a missing numeric value, which is always displayed as a dot (unless specified otherwise with the MISSING system option).
Thank you for explaining the 'IFN' and IFC. and you are correct.
As others have explained you are using the wrong function for creating a character value.
But why use those confusing IFN/IFC functions any way when you can just use normal basic SAS code?
data ddx;
set dd;
if not missing(aval) then AVALC = put(aval,f2.);
run;
Or perhaps since F2. can only display values between -9 and 99 perhaps use:
data ddx;
set dd;
if -9 <= aval <= 99 then AVALC = put(aval,f2.);
run;
Thank you, Tom. This code was written by an experienced guy ( who retired). So I am handling his code. unfortunately, What happening is the code he wrote creating a '.' in AVALC ( character variable). Which leads to the derivation of new useless records with '.', I am just trying to avoid. and I am new to IFN and IFC. I am learning it through trial and error. Thank you for giving the alternatives. I really appreciate your time.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.