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

Hi all:

 

 I am looking for the NON-missing records in several numeric variables.   I have the codes below.  

 

%let vars = admitdt dischargedt 23hrstay hosp hospdt dob sex antimed rapidRT;

data want;
	set have;
	if &vars. ^= . ;
run;

 

The error message is shown:

WARNING: Apparent symbolic reference VARS not resolved.

ERROR 386-185: Expecting an arithmetic expression.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

 

Please advice how to fix it.   Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Satish_Parida
Lapis Lazuli | Level 10

1. Invalid Variable name 23hrstay

2. cmiss or nmiss is the best way of doing your code

 

Please find the bellow approach.

 

option source source2 mprint;
%macro runit;

%let vars = admitdt dischargedt v_23hrstay hosp hospdt dob sex antimed rapidRT;

%let v_count=%sysfunc(countw(&vars, ' '));

%put &=v_count;

data want;
	set have;
	if 
	%do i=1 %to &v_count.;
		%sysfunc(scan(&vars,&i)) %str(ne .)
		%if &i ne &v_count %then %str(or);
	%end;;
run;

%mend runit;

%runit;

Please let us know if it worked for you.

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

Hi all:

 

 I am looking for the NON-missing records in several numeric variables.   I have the codes below.  

 

%let vars = admitdt dischargedt 23hrstay hosp hospdt dob sex antimed rapidRT;

data want;
	set have;
	if vars. ^= . ;
run;

 

The error message is shown:

WARNING: Apparent symbolic reference VARS not resolved.

ERROR 386-185: Expecting an arithmetic expression.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

 

Please advice how to fix it.   Thanks.


 

 

Two issues. One is that I think you want to use

 

if &vars.

but you left out the ampersand.

 

 

The second issue is that even if you put in the ampersand, you still must have the macro variable resolve and produce LEGAL VALID SAS code. So if you use

 

if &vars. ^= . ;

This does not produce legal valid SAS code, because &vars is replaced with its value, and then

 

if admitdt dischargedt 23hrstay hosp hospdt dob sex antimed rapidRT ^= .;

is not legal valid SAS code. You have to fix the IF statement in such a way that it becomes legal valid SAS code. I'd give you an example, but I don't really know what you want as the output, and so I'd need to understand that before I can give you an example.

 

In any event, the best advice I can give you, and really the only path forward, is to start with a simple example, such as the case where you have only 2 variables, ADMITDT and DISCHARGEDT. Write SAS code for these two variables, without macros, run it so that you can confirm it works properly, and then creating and using a macro variable will be much simpler. Without working SAS code, you will not get far.

 

 

 

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

The variable name shown here are fake name I create.   I did try the ones with separated order.   For example, dob ^=. & sex ^=. .    With the code above is working.  However, I would like to create a macro variable list because I am not just look for two variables, a BUNCH of them instead.  Actually, I did add & in my actual codes.

PaigeMiller
Diamond | Level 26

As I said, @ybz12003 , the place to start is to get an example working with just two variables and NO MACROs. Confirm that the code works. Show us the working code. Then creating macros will be much much easier.

 

Also, given the brief example you show, there's really no need for macros here at all. Perhaps this is part of a much more complex problem where you might need macros, but this hasn't been demonstrated.

--
Paige Miller
Reeza
Super User
Look into NMISS and CMISS functions.
Kurt_Bremser
Super User

Start with working SAS code, then replace the necessary parts with macro variables to make it dynamic.

Use

options mprint mlogic symbolgen;

when debugging macro code, and (Maxim 2) read the log. In its entirety.

ybz12003
Rhodochrosite | Level 12

Below the codes works.   Basely, I need to list all of the variables name. 

 

data want;
	set have;
	if admitdt ^= . 
		or dischargedt ^= . 
		or stay ^= . 
		or hosp ^= . 
		or hospdt ^= . 
		or dob ^= . 
		or sex ^= . 
		or disd ^= . 
		or antivir ^= . 
		or cxr ^= . 
		or rapid ^= . ;
run;

My final goal is simplifying my typing the variables one by one.   Thanks.

Reeza
Super User

Use NMISS(). It will count the number of missing and you don't want any.  This will work for a single variable or a list of variables.

 

if nmiss( of &VariableList.) > 0 then ....;

@ybz12003 wrote:

Below the codes works.   Basely, I need to list all of the variables name. 

 

data want;
	set have;
	if admitdt ^= . 
		or dischargedt ^= . 
		or stay ^= . 
		or hosp ^= . 
		or hospdt ^= . 
		or dob ^= . 
		or sex ^= . 
		or disd ^= . 
		or antivir ^= . 
		or cxr ^= . 
		or rapid ^= . ;
run;

My final goal is simplifying my typing the variables one by one.   Thanks.


 

PaigeMiller
Diamond | Level 26

These are all numeric variables ... so ... following the suggestion given by @Reeza 

 

data want;
    set have;
    if nmiss(admitdt,dischargedt, /* you type the rest of the variable names */)=0;
run;

Alternatively, you can use SAS variable lists

for example if your variables are consecutive in the data set, then this ought to work:

 

data want;
    set have;
    if nmiss(of admitdt--rapid)=0;
run;

By the way, don't you mean 

 

if admitdt ^=. AND dischargedt^=. 

 

--
Paige Miller
Astounding
PROC Star

You will always have to  type the variable names once, if only to complete a %LET statement.  If this code is working code, and this is the only place you need a list of variables, you could use:

 

data want;

set have;

if n(admitdt, dischargedt, stay, hosp, hospdt, dob, sex, disd, antivir, cxr, rapid) > 0;

run;

 

All the variables must be numeric for this to work.  (But they all would need to be numeric for your original code to be valid as well.)

 

So, if some variables are character, changes are needed.  If the list of variables needs to be used elsewhere in your program, changes are required.  If none of those conditions applies, you should be all set.  But if changes are needed, you will need to explain more.

Satish_Parida
Lapis Lazuli | Level 10

1. Invalid Variable name 23hrstay

2. cmiss or nmiss is the best way of doing your code

 

Please find the bellow approach.

 

option source source2 mprint;
%macro runit;

%let vars = admitdt dischargedt v_23hrstay hosp hospdt dob sex antimed rapidRT;

%let v_count=%sysfunc(countw(&vars, ' '));

%put &=v_count;

data want;
	set have;
	if 
	%do i=1 %to &v_count.;
		%sysfunc(scan(&vars,&i)) %str(ne .)
		%if &i ne &v_count %then %str(or);
	%end;;
run;

%mend runit;

%runit;

Please let us know if it worked for you.

ybz12003
Rhodochrosite | Level 12

Thank you so much, !   Your code is awesome!  Smiley Happy

ybz12003
Rhodochrosite | Level 12

Thanks for all of the expertise advice.  I learn a lot .

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 12 replies
  • 1801 views
  • 2 likes
  • 6 in conversation