简体   繁体   中英

How do I loop over _char_ data SAS

%macro var_in_list(z);
proc contents data=&z. noprint out=cont(keep= name);
run;
proc sql noprint;                              
 select Name  into :VarList separated by ' '
 from cont;
quit;
proc sql noprint;
select count(*) into :count from cont; quit;
%put &count.;
%put &VarList.;
%let a=;

%let finish=%sysfunc(countw(&VarList));
  %do i = 1 %to &finish;
  %put var_&i= %scan(&varlist., &i, " ");
    %ordinal(var_&i);
  %end;

/*%do j=1 %to &finish;*/
/*  %ordinal(a_:);*/
/*%end;*/

%mend var_in_list;

&varlist.=WOE_ASSET_TYPE WOE_CURRENT_ASSETS WOE_CURRENT_LIABILITIES_1 WOE_DEPRECIATION_1 
            WOE_DebtAmount WOE_Delta_TL WOE_Delta_TS WOE_Delta_interest WOE_Delta_npbt WOE_Delta_taxes WOE_EBITDA_COVERAGE WOE_EFA 
            WOE_EPP WOE_IsCodebtor WOE_NACE WOE_OutstandingAmount WOE_PPLTL WOE_PPTA WOE_PRODUCT WOE_P_price WOE_TEENOR WOE_T_L1 
            WOE_max_LD_1 WOE_max_LD_2 WOE_max_LD_3 WOE_max_LD_4 WOE_max_LD_5 WOE_max_LD_6 WOE_max_LD_7 WOE_max_LD_8 WOE_max_LD_9 
            WOE_max_LD_10 WOE_max_LD_12 delnum percbad tot

I want:

%ordinal(WOE_ASSET_TYPE); %ordinal(WOE_CURRENT_ASSETS); etc.

I'm just trying to automate a simple process due to lazyness.

You are referencing a macro variable you never create. You don't really need any macro varaiable. You can just pass the results of the scan to the macro call.

%do i = 1 %to &finish;
  %ordinal(%scan(&varlist., &i, %str( ) ));
%end;

Note your call to %SCAN() is setting both space and double quote as the delimiter characters. It will not have any impact unless some of the variable names actually include double quote characters.

Tom answered how to get your macro to work, but I'll tell you how to not use a macro.

proc sql noprint;                              
 select Name  into :VarList separated by ' '
 from cont;
quit;

That's what, 90% of what you want? Let's just modify that.

What you want is %ordinal( then the value for name then ) , for each row, right? We can cats that, no problem, right in the select !

proc sql noprint;                              
 select cats('%ordinal(',Name,')')  into :OrdCallList separated by ' '
 from cont;
quit;

Now you just call:

&ordcalllist.

Directly in a line of code all by itself - and it will call your macro for you. I don't include the ; here as it's probably not needed - macros don't generally require them - but if for some reason you do need it, just add it to the cats at the end - cats('%ordinal(',name,');')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM