BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mcook
Quartz | Level 8

I have the following code

 

data Item_Import;
     SET Item_Import(RENAME=(Item_Score=cItem_Score));
          Item_Score=input(cItem_Score,3.);
          drop cItem_Score;
run;

 

Item_Import is already numeric, but it may not always be so for other datasets on which i may want to run this code.  

running it when Item_Import is already numeric results in it becoming populated with '.' 

 

how do i get it to keep its values after conversion?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why would you need to "convert" a number to a number? Just assign the value to the new variable.

new_number = old_number ;

The first argument to the INPUT() function is a character value.  If you call it with a number there SAS will convert the number into a string for you using the BEST12. format.  If you then try to read just the first three characters then unless the value is very large you will be reading the leading spaces which would explain why all of the values are missing.

 

If you want code that will work whether the variable is numeric or character try this:

   Item_Score=input(cats(cItem_Score),32.);

The CATS() function will convert either numeric or character values to string removing any leading or trailing spaces.  Use the width of 32 for the informat. That is the maximum width allowed.  It does not matter whether the string is shorter than the informat width or not.

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

This can be handled with macro %IF-%THEN-%ELSE. First you test to see if Item_score is numeric or character. If it is character, you execute the INPUT function. Otherwise you do not execute the input function. The code below is untested and is only valid in SAS 9.4M5 or later

 

/* UNTESTED CODE -- RUNS ON SAS 9.4M5 or LATER */
proc contents data=item_import noprint out=_contents_;
run;
proc sql noprint;
    select type into :type from _contents_ 
        where name='Item_Score';
quit;

data Item_Import2;
     SET Item_Import;
     %if &type=2 %then %do; cItem_Score=input(Item_Score,3.); %end;
     %else %do; cItem_score=Item_Score; %end;
run;

 

--
Paige Miller
Tom
Super User Tom
Super User

Why would you need to "convert" a number to a number? Just assign the value to the new variable.

new_number = old_number ;

The first argument to the INPUT() function is a character value.  If you call it with a number there SAS will convert the number into a string for you using the BEST12. format.  If you then try to read just the first three characters then unless the value is very large you will be reading the leading spaces which would explain why all of the values are missing.

 

If you want code that will work whether the variable is numeric or character try this:

   Item_Score=input(cats(cItem_Score),32.);

The CATS() function will convert either numeric or character values to string removing any leading or trailing spaces.  Use the width of 32 for the informat. That is the maximum width allowed.  It does not matter whether the string is shorter than the informat width or not.

ballardw
Super User

@mcook wrote:

I have the following code

 

data Item_Import;
     SET Item_Import(RENAME=(Item_Score=cItem_Score));
          Item_Score=input(cItem_Score,3.);
          drop cItem_Score;
run;

 

Item_Import is already numeric, but it may not always be so for other datasets on which i may want to run this code.  

running it when Item_Import is already numeric results in it becoming populated with '.' 

 

how do i get it to keep its values after conversion?


Highlighted text indicates your process is flawed. I would guess this is relying on proc import or wizard to bring in data. If the files are supposed to be the same structure, i.e. variable names and types, then you really need to learn an approach that makes each set the same when brought into SAS. The wizards and/or Proc Import will make guesses based on the content of the data. Depending on the exact approach you are using as few as 20 records are examined to set properties which can result in variable changing types from file to file with variables that are not always populated for every record or have values that sometimes contain all numeric digits and sometimes characters other than digits, differing lengths of character variables and sometimes odd treatment of date, time or datetime values.

 

Note that use of the construct:

Data somename;

    set somename;

   <other code>

run;

can be very dangerous in terms of recoding data. This completely overwrites the source the data set and if you have a logic problem the source data may not be recoverable and you have go back to reading the data.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 768 views
  • 3 likes
  • 4 in conversation