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

 

Hi all,

How to modify the code to run successfully?

I want to use &faa in one data set.

%scan(symget('faa'),&i,|) can't run successfully too.

%macro ff;
data a;
  if _n_=1 then do; 
     aa='f|dd|ff';
     call symputx('faa',aa);
  end;
  set xx;
  %let i=1;
  %do %until (%scan(&faa,&i,|) = );
     %scan(&f,&i,|)='1';
     %let i=%eval(&i+1);
  %end;
run;

%mend;
%ff;

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You have a basic macro timing problem. CALL SYMPUTX works at data step execution time, but the macro code is resolved while the macro creates the code that will later be compiled into a data step, so it will try to fetch the macro variable long before it is created.

The deeper issue here is that you try to abuse the macro language for something it is not meant to do: manipulate data.

 

To keep values across observations, use a RETAINed variable, or set a macro variable before the data step. To present valid code, we first need to know the data you start with, and what you want to get out of it.

Please supply an example dataset in a data step with datalines, and the expected result from it.

 

 

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

Anything macro executes before the SAS data step and that's why you've got a timing issue here. The macro %do loop executes before the SAS data step creates macro variable &faa.

 

Please explain what you're actually trying to do. There is a good chance that you don't need macro code at all.

andreas_lds
Jade | Level 19

What do you want to achieve? It seems as if you want to to set the variable "f", "dd" and "ff to 1. All  variables seem to be alphanumeric, right? If yes, you don't need macro-code, but an array:

data a;
   set xx;

   array vars[3] f dd ff;

   do i = 1 to dim(vars);
      vars[i] = '1';
   end;

   drop i;
run;
Kurt_Bremser
Super User

You have a basic macro timing problem. CALL SYMPUTX works at data step execution time, but the macro code is resolved while the macro creates the code that will later be compiled into a data step, so it will try to fetch the macro variable long before it is created.

The deeper issue here is that you try to abuse the macro language for something it is not meant to do: manipulate data.

 

To keep values across observations, use a RETAINed variable, or set a macro variable before the data step. To present valid code, we first need to know the data you start with, and what you want to get out of it.

Please supply an example dataset in a data step with datalines, and the expected result from it.

 

 

Lee_wan
Obsidian | Level 7

Thanks a lot.

I put the step of creating macro variable before this data set.

But I have a new question.

why I use double quotation marks in hash then the log generates issue?

Lee_wan_2-1593589764834.png

 

When I replace them with single quotes('cmseq'), there's no problem.

ChrisNZ
Tourmaline | Level 20

Your double quotes are curly quotes and not typed from the keyboard. Use straight quotes. Type them yourself in the SAS editor.

Lee_wan
Obsidian | Level 7
data cm;
    if _N_=1 then do;
        dcl hash h(multidata:'Y');
        h.definekey('usubjid', "cmseq");
        h.definedata('qnam', 'qval');
        h.definedone();

        do until(eof);
          set sdtm.suppcm end=eof;
          cmseq=input(idvarval,best.);
          rc=h.add();
        end;
    end;

It's not about double quotes.

ChrisNZ
Tourmaline | Level 20

Timing issue as you were told.

When the data step compiles, all variables must be known.

So:

%scan(&faa,&i,|)

must be resolved.

This is of course impossible in your scenario as &f is unknown at compile time.

Also note the inconsistency: you seem to have confused variables &f and &faa .

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
  • 7 replies
  • 640 views
  • 0 likes
  • 5 in conversation