DATA Step, Macro, Functions and more

get substring from variablename

Reply
Contributor
Posts: 20

get substring from variablename

Hi,

   I'm getting csv file as input......In  that three columns are varying all time.

   So, i'm using below code to handle that one.

  From those variables i want to pick part of the string.,I'm trying below code

  getting error....Can somebody help me how to handle this scenario.

  from this variable Pack Units-jun'11 ....i have to pick  just jun'11.

  Here is error msg:

 

rename

26 ! &vara1=substr(compress(&vara1),length(&vara1),-5);

_

22

ERROR: Alphabetic prefixes for enumerated variables (PackUnits-jun11) are different.

2 The SAS System 16:18 Tuesday, August 30, 2011

26 (compress(&vara1),length(&vara1),-5);

_

76

ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;.

ERROR 76-322: Syntax error, statement will be ignored.

27 run;

Proc contents data = SASUSER.TEST12 out = contents1;
run;

Proc sql;
select distinct name into  : vara1-:vara3 from contents1 where name not in ('Account Manager','CORPORATE PARENT NAME')
order by VARNUM;
quit;


data final ;
set SASUSER.TEST12;
rename &vara1=substr(compress(&vara1),length(&vara1),-5);
run;

Thanks,

reddy.

Super User
Posts: 10,028

get substring from variablename

How about:

options validvarname=any;
data test;
"Pack Units-jun'11"n= 'asdasdf';
run;
%let var1= %str(Pack Units-jun%'11) ;
proc datasets library=work memtype=data;
 modify test;
 rename "%unquote(&var1)"n="%scan(&var1,-1,%str(-))"n;
quit;
%put _user_;

Ksharp

Valued Guide
Posts: 2,177

get substring from variablename

reddy

with what code do you read the file into SAS?

Whatever it is,  that might have an option to create column names that are easier to work with.

Looks like the existing routine is generating column names that conform to option validvarnane= any;

These are awkward to handle in the macro language but simpler in a data step.

like:

data _null_ ;

  if _n_ = 1 then call execute(' data final ; set sasuser.test12; rename ' ) ;

  set contents  end= eof ;

  where lowcase(name)=: 'pack units-'  ;

  length newA1 $32 ;

  newA1 = scan( name, -1, '-' ) ; *picking out the suffix after the - ;

  call execute( quote(trim(name)) !! 'n  =  ' !! quote( trim(newA1)) !! 'n'  ) ;

  if eof ;

  call execute( ' ; run ;' ) ;

run ;

Ask a Question
Discussion stats
  • 2 replies
  • 1360 views
  • 0 likes
  • 3 in conversation