DATA Step, Macro, Functions and more

Using Field Format to Run a Macro

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 83
Accepted Solution

Using Field Format to Run a Macro

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.


Accepted Solutions
Solution
‎08-04-2015 09:55 AM
Super User
Posts: 9,687

Re: Using Field Format to Run a Macro

&Branch_chk is a variable name or value ?


if it was a value , use %datatype

%if %datatype(&Branch_chk) = N %then %do;

View solution in original post


All Replies
Super User
Posts: 10,538

Re: Using Field Format to Run a Macro

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.

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Using Field Format to Run a Macro

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.

Super User
Posts: 10,538

Re: Using Field Format to Run a Macro

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?

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Using Field Format to Run a Macro

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.

SAS Super FREQ
Posts: 684

Re: Using Field Format to Run a Macro

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

Solution
‎08-04-2015 09:55 AM
Super User
Posts: 9,687

Re: Using Field Format to Run a Macro

&Branch_chk is a variable name or value ?


if it was a value , use %datatype

%if %datatype(&Branch_chk) = N %then %do;

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Using Field Format to Run a Macro

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 381 views
  • 0 likes
  • 4 in conversation