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

Hi All,

I have a columns name like below:

Description 201201  201202  201203  201204  201205  201301  201302  201303  201304..........etc.

Now i want to change column name dynamically from 201201 to 201304

into following format

Jan-2012  Feb-2012 Mar-2012 Apr-2012 May-2012 Jan-2013 Feb-2013 Mar-2013 Apr-2013.......etc.

 

Thanks,

Harsh

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Sorry, my mistake. I pasted it wrong. This is the code that should be executed:

data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

data _null_;
  set work.have;
  array vars '201201'n -- '201203'n;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

12 REPLIES 12
PeterClemmensen
Tourmaline | Level 20

Do you want to do this in your actual SAS data or just in Proc Report?

harshpatel
Quartz | Level 8

Hi,

Actually i want to do in actual data set

But you can guide me in both the way if possible

yabwon
Onyx | Level 15

Hi,

You can't have digit as a first symbol of variable name (unless you have the `validvarname=any` option enabled). 

If validvarname is set to `any` you will have to call to your variables names with quotes ended with `n` in your code, e.g. "201201"n.

Hyphen (`-`) is also not allowed in standard session setting, I would reccomend replace it with underscore(`_`), other wise you will have to use the "..."n approach.

With that "warning" said, your code could be something like:

options validvarname=ANY;
data work.have;
"201201"n = 1;
"201202"n = 1;
"201203"n = 1; 
"201204"n = 1; 
"201205"n = 1; 
"201301"n = 1; 
"201302"n = 1; 
"201303"n = 1; 
"201304"n = 1;
run;

data _null_;
  set work.have;
  array vars _numeric_ ;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



harshpatel
Quartz | Level 8

Thanks for your response,

 But when i am submiting below program with your guidance then i am getting below error.

data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

 

One of my column name id automatically renamed as (- .) , but it should named id only

yabwon
Onyx | Level 15

Replace this part:

array vars _numeric_ ;

with something like this:

array vars '201201'n -- '201203'n;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



harshpatel
Quartz | Level 8

Thanks,

But now description column is showing (- .)

Please find the below screenshot

harshpatel_0-1607945446461.png

 

yabwon
Onyx | Level 15

I see correct result. Did you run it like that? 

 

data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

data _null_;
  set work.have;
  array vars _numeric_ ;

  length monyear $ 8 replace $ 32767;
  do over array vars '201201'n -- '201203'n;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;

Are the variables in that exact order in the data set: ID Description '201201'n '201202'n '201203'n ?

What Log says?

 

B

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



harshpatel
Quartz | Level 8

Hi,

It is showing below error

harshpatel_0-1607952161379.png

 

yabwon
Onyx | Level 15

Sorry, my mistake. I pasted it wrong. This is the code that should be executed:

data have;
retain ID Description '201201'n '201202'n '201203'n 0;
run;

data _null_;
  set work.have;
  array vars '201201'n -- '201203'n;

  length monyear $ 8 replace $ 32767;
  do over vars;
    monyear = put(input(vname(vars),YYMMN6.),MONYY7.);
    monyear = substr(monyear,1,3) !! "-" !! substr(monyear,4,4);
    put monyear =;

    replace = cat(strip(replace), " '", strip(vname(vars)), "'n = '", monyear, "'n ");
  end;

  call symputX("replace",replace,"G");
  stop;
run;

title "before";
proc print data = work.have;
run;

proc datasets lib=work nolist;
modify have;
  rename 
    &replace.
  ;
run;
quit;

title "after";
proc print data = work.have;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



harshpatel
Quartz | Level 8

Thanks,

Great, it works for me

Tom
Super User Tom
Super User

Why do you want to go from one set of bad names to another?   

Anyway let's setup an example dataset since you didn't provide one.

options validvarname=any;
data have;
 length ID $12  Description $20 '201201'n-'201212'n  8;
run;

So to convert those YYYYMM strings into actual DATE values use the YYMMN informat.  Then to generate the MON-YYYY strings you can use the MONNAME and YEAR formats.  You can use the DICTIONARY.COLUMNS metadata view to find the names.  

proc sql noprint;
select catx('-',put(input(name,yymmn6.),monname3.),put(input(name,yymmn6.),year4.)) as date
     , catx('=',nliteral(name),nliteral(calculated date))
  into :renames,:renames separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
    and 0=notdigit(trim(name))
;
quit;

Resutls:

 %put &=renames ;
RENAMES="201201"N="Jan-2012"N "201202"N="Feb-2012"N "201203"N="Mar-2012"N "201204"N="Apr-2012"N
"201205"N="May-2012"N "201206"N="Jun-2012"N "201207"N="Jul-2012"N "201208"N="Aug-2012"N
"201209"N="Sep-2012"N "201210"N="Oct-2012"N "201211"N="Nov-2012"N "201212"N="Dec-2012"N

Now you use that list in a RENAME statement (or the RENAME= dataset option) to rename the variables in the next step you run.  Or to rename in place use PROC DATASETS to modify the dataset and change the names without re-writing the actual data.

proc datasets lib=work nolist;
  modify have;
    rename &renames ;
  run;
quit;

But even with the new names your dataset is still not very useful.  Instead you should transpose the data so that the DATE values are stored as data and not as metadata (variable NAME).  For example you could call the new variable MONTH since it contains month values. Then give the COL1 that PROC TRANSPOSE will create to store the values you have the variables with dates as names a name that actually reflects what the values mean.

proc transpose data=have out=want;
  by id description;
run;
data want ;
  set want;
  month= input(_name_,yymmn6.);
  format month monyy7. ;
  rename col1 = Real_Name ;
run;
harshpatel
Quartz | Level 8

Thanks,

Superb, it works for me

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 12 replies
  • 1544 views
  • 0 likes
  • 4 in conversation