SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shawn08
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Cynthia_sas
SAS Super FREQ

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

ballardw
Super User

Why is there an %END statement in there?

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1431 views
  • 4 likes
  • 4 in conversation