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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

2 REPLIES 2
Reeza
Super User

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.

Patrick
Opal | Level 21

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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