BookmarkSubscribeRSS Feed
DanielQuay
Quartz | Level 8

I give up, this one doesn't make sense to me.  I have a variable in a dataset that is all missing.

I need it to recode to 99, the code for does not collect.  I've tried a number of variations.

Dropping the variable beforehand and then adding it back, If/Then logic.  Everything keeps generating * when I run a proc freq.

 

Here's my basic code:

 

data want;

     set have;

          variable = 99;

run;

 

9 REPLIES 9
Reeza
Super User
What is the type of the variable (character/numeric) and can you show the log please?
DanielQuay
Quartz | Level 8

Well that's probably it, it's a character variable. 

I will try to get you the log on that, but I have to clean up the log a bit first.

Reeza
Super User
Then your code should read variable = '99'; at minimum.
DanielQuay
Quartz | Level 8

I think this is the code you're wanting from the Log Reeza.

NOTE: There were 69871 observations read from the data set WORK.MODIFY.
NOTE: The data set WORK.MOD2 has 69871 observations and 186 variables.
NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted
by the "BEST" format.
NOTE: DATA statement used (Total process time):
real time 0.27 seconds
cpu time 0.23 seconds

 

There is not formatting applied in the creation of this one. 

Here is what I get when I run the contents:

78
HSMRNT_PRVD_TPChar1$1.$1.
Quentin
Super User

That shows that your variable does have the $1 format attached, as Tom predicted.  Please see above suggestions from Tom and me about how to remove the format.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Tom
Super User Tom
Super User

So you have a character variable that is only one byte long.  There is no way to store the two byte string '99' into that variable.

You will need to make a NEW variable to change the length (or at least define the length before referencing the old variable via the SET statement).  

 

What value is it that you want to change to '99'?  If cannot be a numeric missing value since the variable is character and not numeric.

 

And also (perhaps actually most importantly) why would you WANT to convert a missing value to a non-missing value?  If you did make the variable numeric then storing 99 instead of . (or any of the other 27 special missing values) would make using variable HARDER.  You could not use MAX or MIN or MEAN or other statistical operations on it without first eliminating the 99's.

 

Is it possible you created this dataset by using PROC IMPORT?  That procedure when reading from a text file or an Excel file will define any variable with all missing values as character with a length of 1 since that takes less space to store than a number.

Quentin
Super User

Since you mention PROC freq, check that you don't have a format changing the value displayed.

 

proc freq data=want;
  tables variable;
  format variable;  *remove any format attached;
run;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
ballardw
Super User

@DanielQuay wrote:

I give up, this one doesn't make sense to me.  I have a variable in a dataset that is all missing.

I need it to recode to 99, the code for does not collect.  I've tried a number of variations.

Dropping the variable beforehand and then adding it back, If/Then logic.  Everything keeps generating * when I run a proc freq.

 

Here's my basic code:

 

data want;

     set have;

          variable = 99;

run;

 


Show us the results of Proc Contents for your Have data set. That will help reduce the number of things that we have to guess at. NOT the Want data set, the HAVE, though if the code is as you show there really shouldn't be much difference.

Tom
Super User Tom
Super User

Check what display format you have attached to the variable.

I can recreate what you seem to be describing by just attaching a format that  uses a width that is too small to display 99.

data test;
  variable=99;
  format variable 1.;
run;

proc freq data=test;
  tables variable;
run;
The FREQ Procedure

                                     Cumulative    Cumulative
variable    Frequency     Percent     Frequency      Percent
-------------------------------------------------------------
       *           1      100.00             1       100.00

Note that most SAS variables do NOT need to have a format attached.  The exception is DATE, TIME and DATETIME values that are hard for humans to understand without using a format to display them.  If you want to just remove the formats from a variable (or multiple variables) then use a FORMAT statement that lists variables but does not include any format specification after the list.  You can use the _ALL_ keyword to remove the formats from ALL of the variables.  You could then add a second format statement to add back any formats that are needed.

format _all_ ;
format datevar date9.;

 

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
  • 9 replies
  • 1200 views
  • 1 like
  • 5 in conversation