BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

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;
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

View solution in original post

9 REPLIES 9
FreelanceReinh
Jade | Level 19

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

SASuserlot
Barite | Level 11

Thank you, Not sure how I missed that log Note. It worked. Thank you

PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller
SASuserlot
Barite | Level 11

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.

SethHunt
Calcite | Level 5

In your code, use "AVALC = ifn(aval = ., '', left(put(aval, f2.)));", it will assign a blank value when aval is missing.

Kurt_Bremser
Super User

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

SASuserlot
Barite | Level 11

Thank you for explaining the 'IFN' and IFC. and you are correct.

Tom
Super User Tom
Super User

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;
SASuserlot
Barite | Level 11

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 9 replies
  • 752 views
  • 2 likes
  • 6 in conversation