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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 4247 views
  • 4 likes
  • 3 in conversation