DATA Step, Macro, Functions and more

Pass a the value of VTYPE to macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Pass a the value of VTYPE to macro variable

Dear All,

I am trying to pass the return value of VTYPE to a macro variable, but was not successful.  The code and output are as the following.  Please give a suggestion:

CODE:

DATA test;

  SET LOCFRST;

  IF VTYPE(ENRLL)= 'C' THEN DO;

       %LET VarTyp=VTYPE(ENRLL);

       VarTyp2 = VTYPE(ENRLL);

       PUT "NOTE: VTYPE = &VarTyp = VTYPE(ENRLL), Another test: " VarTyp2=;

  END;

RUN;

LOG:

NOTE: VTYPE = VTYPE(ENRLL) = VTYPE(ENRLL), Another test: VarTyp2=C

Why the 'C' did not assign to Macrovariable VarTyp?

Thanks,

Abdu.


Accepted Solutions
Solution
‎05-16-2014 08:38 PM
Respected Advisor
Posts: 3,902

Re: Pass a the value of VTYPE to macro variable

Adding to what posted:

You will need to read up a bit of how SAS macro language and SAS data step interact. You have a timing issue with your %let statement and it executes before the data step actually iterates through the data.

Using a "call symput" instead will do the job - only execute it once though "IF _n_=1 and VTYPE(ENRLL)= 'C' THEN DO;"

data test;
  set sashelp.class;
  length vartyp2 $1;
  retain vartyp2;

  if _n_=1 and vtype(name)= 'c' then
    do;
      vartyp2 = vtype(name);
      call symput ('vartype', vtype(name));
    end;

run;

%put vartype is: &vartype;

View solution in original post


All Replies
Super User
Posts: 17,912

Re: Pass a the value of VTYPE to macro variable

In a data step it's easier to use call symput to create macro variables.

The following should work.

call symput ('vartype', vtype(enrll));

How does the macro language know if you want to store the text "VTYPE(ENRLL)" or the resolved value of said function?

You can tell it to resolve the function using %sysfunc, %eval depending on what you're doing.

Solution
‎05-16-2014 08:38 PM
Respected Advisor
Posts: 3,902

Re: Pass a the value of VTYPE to macro variable

Adding to what posted:

You will need to read up a bit of how SAS macro language and SAS data step interact. You have a timing issue with your %let statement and it executes before the data step actually iterates through the data.

Using a "call symput" instead will do the job - only execute it once though "IF _n_=1 and VTYPE(ENRLL)= 'C' THEN DO;"

data test;
  set sashelp.class;
  length vartyp2 $1;
  retain vartyp2;

  if _n_=1 and vtype(name)= 'c' then
    do;
      vartyp2 = vtype(name);
      call symput ('vartype', vtype(name));
    end;

run;

%put vartype is: &vartype;

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 1125 views
  • 4 likes
  • 3 in conversation