My SAS Tutorials

Friday 14 March 2014

SAS INTERVIEWS CONCEPTUAL QUESTIONS XIX

We have a SAS Data Set "One" in that we have 100 variables starting from "XYZ_" like:

XYZ_VAR1
XYZ_VAR2
XYZ_VAR3
XYZ_VAR4
.
.
.
.
.
.
XYZ_VAR99
XYZ_VAR100

We need to recreate the SAS Data Set "One" with variables Names:

VAR1
VAR2
VAR3
VAR4
VAR5
VAR6
.
.
.
.
.
.
VAR99
VAR100

Most efficient program will be appreciated.

Now prepare your Base SAS and Advance SAS Certification Exams with Online Mock tests:  http://exam.sankhyana.com



For training related info kindly mail us at info@sankhyana.com
www.sankhyana.com
 

7 comments:

  1. Get the variables from dictionary tables and use tranwrd function and replace "xyz_" value with a blank and then transpose the data with the ID statement.

    ReplyDelete
  2. proc sql noprint;
    select name,count(distinct name) into :varname n separated by ' '
    from dictionary.columns
    where libname='WORK' and memname='ONE';
    quit;



    proc sql noprint;
    select count(name) into :n
    from dictionary.columns
    where libname='WORK' and memname='ONE';
    quit;


    %macro a;

    data one;
    set one;
    %do i=1 %to n;
    %let a = %scan(&varname,&i);
    %if %length(&a) %then %do;
    rename &a = %substr(&a,5);
    %end;
    %end;
    run;
    %mend a;
    %a;


    ReplyDelete
    Replies
    1. data reset_var;
      set k (rename=(xyzvar1-xyzvar100=var1-var100));
      run;

      Delete
    2. This one is easy to work. Good.

      Delete

  3. %macro test;
    data one;
    A_xyz='a';
    A_jkl='b';
    A_pqr='c';
    A_mnp='d';
    run;
    data two;
    set sashelp.vcolumn(keep=name libname memname);
    where upcase(libname)='WORK' and upcase(memname)='ONE';
    y=substr(name,3);
    /*callsymputx('x'||left(_n_),substr(name,5));*/
    keep name y;
    run;
    data _null_;
    set two;
    call symputx('x'||left(_n_),name);
    call symputx('y'||left(_n_),y);
    run;
    data one;
    set one;
    %do i=1 %to 4;
    rename &&x&i=&&y&i;
    %end;
    run;
    proc print data=&syslast;
    run;
    %mend test;
    %test




    ReplyDelete