Hello,
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;
data employee2;
set employee1;
run;
%end;
Any ideas? I appreciate any thoughts on this.
&Branch_chk is a variable name or value ?
if it was a value , use %datatype
%if %datatype(&Branch_chk) = N %then %do;
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
from dictionary.columns
where libname= "&lib"
and memname="&mem"
and name="&var"
;
quit;
%put &variabletype;
Or some other way to look at the dataset. I can think of at least two others.
Ballardw,
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.
Bruno,
Thanks for the links. I'll look into them. The autocall macro looks kinda like what I'm trying to accomplish.
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?
Thanks ballardw,
I was planning on using something along the lines of:
data chk;
set chk;
variable1 = input(variable,$8.);
drop variable;
rename variable1 = variable;
run;
That is, once I'm able to identify the variables I want to reformat.
Ody
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:
SAS-macros/attrv.sas at master · friendly/SAS-macros · GitHub
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
Bruno
&Branch_chk is a variable name or value ?
if it was a value , use %datatype
%if %datatype(&Branch_chk) = N %then %do;
Xia,
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;
data tables1;
set tables1;
NML_NUM1 = put(NML_NUM, $5.);
drop NML_NUM;
rename NML_NUM1 = NML_NUM;
run;
%end;
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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.