DATA Step, Macro, Functions and more

varible name will be for oth and need to make it optional so that if there is variable present then

Reply
Occasional Contributor mj5
Occasional Contributor
Posts: 18

varible name will be for oth and need to make it optional so that if there is variable present then

 

Hi,

I am trying to do the below code

 

 

%let oth=Other;

data test;
   if &oth ne "" then &oth;
run;

I am not sure what the varible name will be for oth and need to make it optional so that if there is variable present then use it else not use.

 

Somehow this program logic doesnt work. Am i missing something. Please advice

Super User
Posts: 10,601

Re: varible name will be for oth and need to make it optional so that if there is variable present t

[ Edited ]

Just resolve the macro variable yourself:

data test;
   if Other ne "" then Other;
run;

and you will see that the code is syntactically invalid and makes no sense at all.

What are you trying to achieve?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor mj5
Occasional Contributor
Posts: 18

Re: varible name will be for oth and need to make it optional so that if there is variable present t

Posted in reply to KurtBremser

I am trying to make a macro variable oth and not sure what the variable name will be for it. and then later in the datastep want to make sure if the variable exists and has no missing observation then assign the macro variable the variable name which will exist

%let oth=other; in case the variable name is other. 

 

if other ne "" then &oth=other;

Super User
Posts: 10,601

Re: varible name will be for oth and need to make it optional so that if there is variable present t

Following your logic, you do exactly nothing, in a quite complicated way:

You set macro variable oth to the value

Other

Then you check if data step variable "other" is not missing, in which case you again set the macro variable oth to

Other

and if not existing or missing, you do nothing, so &oth will keep its value, which is

Other

???

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,862

Re: varible name will be for oth and need to make it optional so that if there is variable present t

"and then later in the datastep want" - macro language is a separate component from datastep language.  Datastep language is the programming language of SAS, macro is an additional textual find and replace system which can help generate datastep code.  The to do not work together, but on after the other, so your statement above can never be done.  Describe your problem, start with some test data, and what you want out at the end.  There are many ways to find out if a variable is missing, however it isn't that often you would need to change your code physically based on this assumption.

Ask a Question
Discussion stats
  • 4 replies
  • 130 views
  • 0 likes
  • 3 in conversation