BookmarkSubscribeRSS Feed
ssv
Fluorite | Level 6 ssv
Fluorite | Level 6

Hi there!

I am facing a problem in Proc format. I was trying to convert a character variable to numeric variable by using the input function and at the same time i am applying a user defined character format to that character variable. but it is showing a warning in the log window.

 

WARNING: The data set WORK.BB may be incomplete. When this step was stopped there were 0 observations and 2 variables.
WARNING: Data set WORK.BB was not replaced because this step was stopped. 

 

The program I submitted is as follows:

 

proc format;
           value $namefmt
           'abc' = 1
          'bbc' = 2
          'ccc' = 3;
run;


data old;
       input name $;
       cards;
       abc
       bbc
       ccc
         ;
run;

 

data new;
       set old;
       newname=input(name,namefmt.);
run;

 

But if i use Put function in place of input function it is working fine with out any warning. But Put function usually converts Numeric to Character. But why is it working for a character variable? 

 

data bb;
       set ab;
       newname=put(name,namefmt.);
run;

 

If i use this program then it is working fine again.

 

data bb;
       set ab;
       newname=input(put(name,namefmt.),best.);
run;

 

Can some one please Clarify this?

Thanks in Advance.

 

7 REPLIES 7
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

try this for the new dataset

 

data new;

set old;

 

newname=name;

format newname $namefmt.;

run;

 

 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

how about apply the $ before the format name.

 

data bb;

set old;

newname=put(name,$namefmt.);

run;

ssv
Fluorite | Level 6 ssv
Fluorite | Level 6

Hello VDD,

Thank you for your reply. 

Actually it is working with out any errors with or with out $ sign. 

--------------------------------------------------------------------------------

data bb;

set old;

newname=put(name,namefmt.);

run;

data bb;

-------------------------------------------------

set old;

newname=put(name,$namefmt.);

run;

 

 

Both of these result in the same output.

But my question was when we are converting a character variable to Numeric we need to use input function. and in this program i am trying to convert a character variable to numeric , (in addition i need to apply a user defined format) But if i use INPUT function it throws an error. but if i use PUT function it is fine(with or without $ sign). So why we are unable to convert character variable to numeric by applying a format using INPUT variable?

ssv
Fluorite | Level 6 ssv
Fluorite | Level 6

Hello KurtBremser

 

proc format;
invalue $namefmt
  'abc' = 1
  'bbc' = 2
  'ccc' = 3;
run;

 

as you advised i have created an informat  and applied in this way.

 

data new1;
           set old;
           newname=input(name,$namefmt.);
run;

 

Yes ! now i get the output. A new variable Newname(Character variable) is created and it contains the data 1 2 and 3. but my question is Why is it a character variable? when i use a input function it should convert to numeric but why is it not converting to numeric? 

 

The same is happening with the Put function. When I use a put function it is applying the format. In general PUT function converts numeric values to character. But here if i use a  put function for a character variable it is working fine without any errors.The resultant new variable is a character again.

 

data bb;
         set old;
         newname=put(name,$namefmt.);
run;

 

Both of these programs are producing the same output. 

Kurt_Bremser
Super User

If you want to have a numeric value, you have to create a numeric informat, so omit the $'s:

data old;
name = 'abc';
run;

proc format;
invalue namefmt
  'abc' = 1
  'bbc' = 2
  'ccc' = 3
;
run;

data new1;
set old;
newname=input(name,namefmt.);
run;
ArtC
Rhodochrosite | Level 12

Although not immediately obvious (and a continuing source of confusion) there is a difference between INFORMATs (used by the INPUT statement and the INPUT function) and FORMATs (used by the PUT statement and the PUT function).  The INVALUE statement in PROC FORMAT creates INFORMATs and you will notice from the previous post by @Kurt_Bremser that the type of the INFORMAT depends on the result (numeric INFORMATS yield a numeric result).  FORMATS created by the VALUE statement are numeric if the incoming value is numeric.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1085 views
  • 1 like
  • 4 in conversation