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.
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;
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.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.
