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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

&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

7 REPLIES 7
ballardw
Super User

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.

Ody
Quartz | Level 8 Ody
Quartz | Level 8

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.

ballardw
Super User

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?

Ody
Quartz | Level 8 Ody
Quartz | Level 8

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.

BrunoMueller
SAS Super FREQ

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

Ksharp
Super User

&Branch_chk is a variable name or value ?


if it was a value , use %datatype

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

Ody
Quartz | Level 8 Ody
Quartz | Level 8

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!

sas-innovate-2024.png

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.

 

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
  • 7 replies
  • 1253 views
  • 0 likes
  • 4 in conversation