Hi,
I have a dataset composed of 1 character (name) and 3 numeric variables (a b N_obs). I want to change a and b values to N/A if N_obs is lower than 10 and name = "X".
Here is my code:
proc report nowd data = data;
column (name a b);
define name / display "Name";
define a / display "A";
define B / display "B";
compute A;
if name = "X" and N_obs <= 10 then A = "N/A";
endcomp;
compute B;
if name = "X" and N_obs <= 10 then B = "N/A";
endcomp;
%end;
run;
Yet, if name = X and N_obs <= 10 then it gives me A and B = .
I guess this is because A and B are numeric variables. But, would there be a way to replace by N/A instead of .?
Thank you in advance
You can't give a character value to a numeric variable. You ought to assign a format to A and B when they are missing, and the format should make the missing appear as "N/A".
I think another problem is that n_obs is not listed in the COLUMNS statement.
UNTESTED CODE
proc format;
value myfmt .='N/A' ;
run;
proc report nowd data = data;
column (name n_obs a b);
define name / display "Name";
define n_obs/display noprint;
define a / display "A" format=myfmt.;
define B / display "B" format=myfmt.;
compute A;
if name = "X" and N_obs <= 10 then A = .;
endcomp;
compute B;
if name = "X" and N_obs <= 10 then B = .;
endcomp;
%end;
run;
You can't give a character value to a numeric variable. You ought to assign a format to A and B when they are missing, and the format should make the missing appear as "N/A".
I think another problem is that n_obs is not listed in the COLUMNS statement.
UNTESTED CODE
proc format;
value myfmt .='N/A' ;
run;
proc report nowd data = data;
column (name n_obs a b);
define name / display "Name";
define n_obs/display noprint;
define a / display "A" format=myfmt.;
define B / display "B" format=myfmt.;
compute A;
if name = "X" and N_obs <= 10 then A = .;
endcomp;
compute B;
if name = "X" and N_obs <= 10 then B = .;
endcomp;
%end;
run;
Hi:
You CANNOT test N_OBS if it is not on the COLUMN statement -- PROC REPORT only has visibility of the variables on the COLUMN statement -- it has NO visibility of variables in the data set. If you want to use a variable, but not show the variable, then you use the NOPRINT option on the DEFINE statement.
Also, the placement of your %end is troublesome because this implies that there is more to your code than you're showing.
Also, since NAME is to the left of A and B in the COLUMN statement, you can test NAME in the COMPUTE blocks, but you cannot test N_OBS unless it were also to the LEFT of A and B:
column name n_obs a b;
And, if N_OBS is numeric, which your code implies, then N_OBS would have to also have a usage of DISPLAY in the DEFINE statement in order for your existing code to work.
Without data, no one can run your code. And with the %end there, your code would cause an error message anyway. And, I completely missed that A and B were numeric...you'll have to make that change with a user defined format and a CALL DEFINE statement.
Cynthia
Why is there an %END statement in there?
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.