BookmarkSubscribeRSS Feed
kariarchie
Calcite | Level 5

I am trying to convert temperatures above 40 degrees celcius into H. 

 

DATA H_TEMP_RECORD2;
MISSING H;
SET H_TEMP_RECORD;
ARRAY VARS {13} _NUMERIC_;

IF VARS(i)>40 THEN VARS(i)=.;
DO i=1 to 13;
END;
DROP i;
RUN;

PROC PRINT DATA=H_TEMP_RECORD2;
RUN;

 

This is what I have so far, but I can't seem to get my temperatures above 40 degrees to say H instead of just a period. Any help would be appreciated! ☺

2 REPLIES 2
ballardw
Super User

First thing: Read your log. That will tell part of what is going on.

I bet that you have at least one error with that code. When you post a question about something not working and your log shows errors or messages you do not understand the copy the entire data step or procedure code from the log and on the forum open a text box with the </> icon above the message window and paste the text there.

 

I think this may be closer to what you attempted:

DATA H_TEMP_RECORD2;
   SET H_TEMP_RECORD;;
   ARRAY VARS {*} _NUMERIC_;

   DO i=1 to dim(vars);
      IF VARS(i)>40 THEN VARS(i)=.;
   END;
   DROP i;
RUN;

options missing='H';
PROC PRINT DATA=H_TEMP_RECORD2;
RUN;

options missing='.';

If you are going to use the list of variables _numeric_ then all the numeric variables will be in the list. If you specify a number in the array definition, such as {13} and that doesn't match the number of _numeric_ variables that will throw an error. The {*} says use the number of variables in the list to set the array size. And possibly list the variables. If you have any DATE, time or datetime values they are very likely to get set to missing when using _numeric_ list in this fashion.

 

Your attempt to check the value was outside the "do i=" loop. So no value was available for the array to use in that assignment (i missing) and invalid as an array index.

 

"missing" in a data step is a function to test whether a value is missing.

I think you were attempting to set the option missing to display 'H' which I have shown above.

Another approach would be to assign a special missing:

DATA H_TEMP_RECORD2;
   SET H_TEMP_RECORD;
   ARRAY VARS {*} _NUMERIC_;

   DO i=1 to dim(vars);
      IF VARS(i)>40 THEN VARS(i)=.H;
   END;
   DROP i;
RUN;

SAS has 27 "special missing" values .A through .Z and ._ that you can use to differentiate different types of "missingness". They will appear in printed output a A through Z or just _ without the dot.

 

 

Patrick
Opal | Level 21

If this 'H' is just for printing then using a format instead of changing the actual values is eventually a better option.

If you want to set the values to missing but then print such missings as 'H' then either use a format as well or alternatively options missing='H'; (but this then impacts the printing of any variable with missings).

data have(drop=_:);
  length row 8;
  array vars{*} 8 t1-t5 tmp1-tmp5;
  do row=1 to 5;
    do _i=1 to dim(vars);
      vars[_i]=ceil(ranuni(1)*60);
    end;
    if row=1 then
      do;
        t1=40;
        t2=40-0.1;
        t3=40+0.1;
      end;
    output;
  end;
  stop;
run;

proc format;
  value temp
    40 <- high = 'H'
    other=[best32.]
  ;
run;

title 'Print Formatted';
proc print data=have;
  format t: tmp: temp.;
run;

title 'Print Not Formatted';
proc print data=have;
run;
title;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 608 views
  • 0 likes
  • 3 in conversation