Parsing words from a SAS macro variable and writing them out to character variables in a data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Parsing words from a SAS macro variable and writing them out to character variables in a data step


This code comes sort of close to working, but the resolve function is not evaluating at the desired time and symget chokes.  Any ideas? The point is to parse each variable name from the covariate list, create a short version of it, and write both the old name and the new short name out to a file for use as a key in further translations.

%macro ShortVNames (NameList, KeyFile);    Data &KeyFile;      Length OrigName $ 32 NewName $ 4; %Let EndofList=0;    %let j=1;    %do %until (&EndofList=1);      %let OrigName=%scan(&NameList, &j,' ');  %if %length(&OrigName)>0 %then %do; OrigName=resolve('&OrigName');    NewName=resolve('V&j'); NewName=right(NewName);    %let j=%eval(&j+1);    output;  %end;  %else %do;    %let EndofList=1; %Let ListLength=%eval(&j-1);  %end; %end;  run; %mend ShortVNames; %LET cov_list2 =      c_concurrent c_dac c_dac_dual c_dwb c_dwb_dual    c_erate_disab c_erate_disab_miss c_female    c_mba c_reppayee c_rural c_shortdur    c_urate  c_veryshortdur    c_auxnodac c_langs c_aime2011 c_S2inelig_geog c_S2inelig_guardian c_zerobenefits c_zeroaime c_mba_aime2011_int c_age_yrsDI_int c_aime2011_aime2011_int ; %ShortVNames(&cov_list2,translator);


Accepted Solutions
Solution
‎03-28-2014 12:39 PM
Respected Advisor
Posts: 3,777

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

I don't think you need RESOLVE function.  See modifications below.

%macro ShortVNames (NameList, KeyFile);
   Data &KeyFile;
   Length OrigName $
32 NewName $ 4;
  
%Let EndofList=0;   
  
%let j=1;   
  
%do %until (&EndofList=1);     
     
%let OrigName=%scan(&NameList, &j,' '); 
     
%if %length(&OrigName)>0 %then %do;
         OrigName=
"&OrigName";   
         NewName=
"V&j";
         NewName=right(NewName);   
        
%let j=%eval(&j+1);   
         output; 
        
%end
     
%else %do;   
        
%let EndofList=1;
        
%Let ListLength=%eval(&j-1); 
        
%end;
     
%end
      run;
  
%mend ShortVNames;
%LET cov_list2 = c_concurrent c_dac c_dac_dual c_dwb c_dwb_dual c_erate_disab c_erate_disab_miss c_female    c_mba c_reppayee c_rural c_shortdur    c_urate  c_veryshortdur    c_auxnodac c_langs c_aime2011 c_S2inelig_geog c_S2inelig_guardian c_zerobenefits c_zeroaime c_mba_aime2011_int c_age_yrsDI_int c_aime2011_aime2011_int ;
options mprint=1;
%
ShortVNames(&cov_list2,translator);

View solution in original post


All Replies
Solution
‎03-28-2014 12:39 PM
Respected Advisor
Posts: 3,777

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

I don't think you need RESOLVE function.  See modifications below.

%macro ShortVNames (NameList, KeyFile);
   Data &KeyFile;
   Length OrigName $
32 NewName $ 4;
  
%Let EndofList=0;   
  
%let j=1;   
  
%do %until (&EndofList=1);     
     
%let OrigName=%scan(&NameList, &j,' '); 
     
%if %length(&OrigName)>0 %then %do;
         OrigName=
"&OrigName";   
         NewName=
"V&j";
         NewName=right(NewName);   
        
%let j=%eval(&j+1);   
         output; 
        
%end
     
%else %do;   
        
%let EndofList=1;
        
%Let ListLength=%eval(&j-1); 
        
%end;
     
%end
      run;
  
%mend ShortVNames;
%LET cov_list2 = c_concurrent c_dac c_dac_dual c_dwb c_dwb_dual c_erate_disab c_erate_disab_miss c_female    c_mba c_reppayee c_rural c_shortdur    c_urate  c_veryshortdur    c_auxnodac c_langs c_aime2011 c_S2inelig_geog c_S2inelig_guardian c_zerobenefits c_zeroaime c_mba_aime2011_int c_age_yrsDI_int c_aime2011_aime2011_int ;
options mprint=1;
%
ShortVNames(&cov_list2,translator);
Occasional Contributor
Posts: 15

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

Wow!  Thanks.  I thought I had tried that first, but I must have gotten something else wrong.  Works like a charm.

--Dave Judkins

Super User
Super User
Posts: 6,499

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

You do not need the macro logic. You can loop over the string in the data step.

%macro ShortVNames (NameList, KeyFile);

data &KeyFile;

  length OrigName $ 32 NewName $ 4;

  keep origname newname ;

  length namelist $32767 ;

  namelist = symget('namelist');

  do j=1 by 1 while(scan(namelist,j,' ')^=' ') ;

    OrigName=scan(namelist,j,' ');

    NewName=cats('V',j);

    NewName=right(NewName);   

    output;

  end;

run;

%mend ShortVNames;

Respected Advisor
Posts: 3,777

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

Your point is valid as far as it goes.  If we assume that these names exist in some data set somewhere, which seems reasonable to me, then a more generally useful way to process them is to use PROC TRANSPOSE.  You get lots of extras with PROC TRANSPOSE.

data covars;
   set sashelp.class;
   retain c_concurrent c_dac c_dac_dual c_dwb c_dwb_dual
         c_erate_disab c_erate_disab_miss c_female   
         c_mba c_reppayee c_rural c_shortdur   
         c_urate  c_veryshortdur   
         c_auxnodac c_langs c_aime2011 c_S2inelig_geog
         c_S2inelig_guardian c_zerobenefits c_zeroaime
         c_mba_aime2011_int c_age_yrsDI_int c_aime2011_aime2011_int
1
         ;
  
run;
%LET cov_list2 = c_concurrent c_dac c_dac_dual c_dwb c_dwb_dual c_erate_disab c_erate_disab_miss c_female    c_mba c_reppayee c_rural c_shortdur    c_urate  c_veryshortdur    c_auxnodac c_langs c_aime2011 c_S2inelig_geog c_S2inelig_guardian c_zerobenefits c_zeroaime c_mba_aime2011_int c_age_yrsDI_int c_aime2011_aime2011_int ;
proc transpose data=covars(obs=0) out=cnames;
   *var &covlist2;
  
var c_:;
   run;
data cname(index=(_name_));
   set cnames;
   length shortname $4;
   shortname = cats(
'V',_n_);
   shortname = right(shortname);
  
run;
proc print;
  
run;
  


Occasional Contributor
Posts: 15

Re: Parsing words from a SAS macro variable and writing them out to character variables in a data step

Another elegant solution.  Thanks!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 234 views
  • 5 likes
  • 3 in conversation