BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
andreas_lds
Jade | Level 19

@acordes wrote:

Can I use the these lines within an array loop???

 

batch_id_dt1=strip(batch_id_dt);
drop batch_id_dt;
rename batch_id_dt1=batch_id_dt;


No, you can't. The variable names can't be inserted by using vname[x].

andreas_lds
Jade | Level 19

@PaigeMiller wrote:

@andreas_lds 

Why is a macro needed? Why not just an array to do the looping?


For moving the values from char to varchar an array is sufficient, but imho not to drop the char-variables and rename the varchar-vars.

With more spare-time i would have suggested a data-null-step writing the code transforming-step without any macro statements.

acordes
Rhodochrosite | Level 12

That's it.

My idea is not so silly in my opinion. 

Here the code that works:

 

%macro varcharer(name=);
	&name.1=strip(&name);
	drop &name;
	rename &name.1=&name;
%mend;

%let var_list=batch_id_dt batch_id;


%macro convert();
data public.want;
    set public.have;
    length batch_id_dt1 batch_id1 varchar(*);

    %do i = 1 %to %sysfunc(countw(&var_list, %str( )));
        %let var = %scan(&var_list, &i, %str( ));
        %varcharer(name= &var);
    %end;
run;
%mend convert;
%convert();

pic10.png

Tom
Super User Tom
Super User

In general it is not a good idea to write macros that reference "magic" parameters.  That is macro variables that are not part of their input.  Or if you must do it then clearly document in the header of the macro what external macro variables it will be referencing.

So just make the macro accept space delimited list of names.

%macro char_to_varchar(varlist);
%local i name ;
%do i=1 %to %sysfunc(countw(&varlist,%str( ),q));
  %let name=%scan(&varlist,%str( ),q);
  length __&name varchar(*) ;
	__&name=strip(&name);
	drop &name;
	rename __&name.=&name;
%end;
%mend char_to_varchar;

Then you can all it the middle of a data step.

data public.want;
  set public.have;
  %char_to_varchar(batch_id_dt batch_id)
run;

If you want to build the list of names into macro variable you can do that.

For example you might do this to create the macro variable VAR_LIST with all character variables whose name starts with BATCH.

proc transpose data=public.have(obs=0 keep=batch: ) out=names;
  var _character_;
run;
proc sql noprint;
  select nliteral(_name_) into :var_list separated by ' '
    from names
  ;
quit;

And then you can use that macro variable in your call to the macro.

data public.want;
  set public.have;
  %char_to_varchar(&var_list)
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 18 replies
  • 2768 views
  • 4 likes
  • 5 in conversation