07-30-2015 02:45 PM
I'm having some difficulty getting a macro to run based on the format of a field. I'm checking the format and if it's numeric then I want to copy my dataset.
I can't find a function that returns any of this information in a way I've been able to evaluate in my macro. I've tried playing with vtype, vtypex, vartype, etc... all with no luck.
Ideally I'd like something like this:
%if %eval (%sysfunc(vartype(&Branch_chk))) = N %then %do;
Any ideas? I appreciate any thoughts on this.
07-30-2015 03:37 PM
Vartype takes as an argument the name of a dataset variable, which from the location in your code stub does not look like your are evaluating a variable in a set. Is &branch_chk supposed to resolve to the name of a variable or are you looking the value of &branch_chk and trying to determine if that is numeric?
If you are looking at the type of a variable you will have to use some method that tells SAS which dataset to determine. The following code looks for a variable named Sex in the dataset Sashelp.Class and assigns the type to the macro variable variabletype. The value returned will be char or num. The libname and memname (data set) must either be in all capital letters or modify the code to use UPCASE. You
%let LIB = SASHELP;
%let MEM = CLASS;
%let var = Sex;
proc sql noprint;
select type into : variabletype
where libname= "&lib"
Or some other way to look at the dataset. I can think of at least two others.
07-31-2015 01:54 PM
In the code I provided I am using the max value of the branch field so I can determine if that field is character or numeric. If its numeric I want to change the field to character, or vice versa for other fields.
My first experiments with the vartype functions would assess each record in my dataset and I wanted to avoid that. I figure taking one example from the field would tell me what I needed to know if only I could somehow assess it.
I'm open to any suggestion. I went down that path because that's all i could think of at the time. I was really hoping for an "if this evaluates to true, then do this" solution.
Thanks for the links. I'll look into them. The autocall macro looks kinda like what I'm trying to accomplish.
07-31-2015 04:00 PM
The previous code I posted will do that for one variable, OR you could modify the code to select all of the variables with type='num' or look for type='char'. The code could also select the type for multiple variables by using
name in (<something that returns a list of variable names>)
though I would tend to ensure they are all either upper or lower case and use the appropriated case function on NAME.
How are you planning on making them change type?
08-03-2015 03:54 PM
I was planning on using something along the lines of:
variable1 = input(variable,$8.);
rename variable1 = variable;
That is, once I'm able to identify the variables I want to reformat.
07-30-2015 04:53 PM
The VARTYPE function can not be used on its own, one has to know for which data set you want to check. See this example code for how to do it:
There is an autocall macro that checks the type of the contents of a macro variable, have a look here: SAS(R) 9.4 Macro Language: Reference, Fourth Edition
08-04-2015 12:08 PM
Thanks for the information.
After some trial and error I was able to get it to work:
%if (%datatyp(&NML_NUM_value) ne Char) %then %do;
NML_NUM1 = put(NML_NUM, $5.);
rename NML_NUM1 = NML_NUM;
I knew there had to be a macro function that checked for the format type. It's odd to me that it's %datatyp and not %datatype. Perhaps that's why i had trouble researching it.
Regardless, All, thank you for the assistance!
Need further help from the community? Please ask a new question.