Help using Base SAS procedures

how to create new variables based on information from old variables?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

how to create new variables based on information from old variables?

Suppose I have following codes:

data new;

set old;

array old_var_array

  • _CHARACTER_;
  • ..................(codes to be inserted)

    ..................(codes to be inserted)

    ..................(codes to be inserted)

    ..................(codes to be inserted)

    ..................

    ..................

    Based on number of character variables ( which can be obtained using dim(old_var_array) in the above example), I want to create equal number of new variables (NewVar1-NewVar#ofChar). How can I do this? thanks.


    Accepted Solutions
    Solution
    ‎09-06-2011 12:42 PM
    PROC Star
    Posts: 7,360

    how to create new variables based on information from old variables?

    Take a look at the method I suggested in a post the other day:

    http://communities.sas.com/message/104323#104323

    View solution in original post


    All Replies
    Solution
    ‎09-06-2011 12:42 PM
    PROC Star
    Posts: 7,360

    how to create new variables based on information from old variables?

    Take a look at the method I suggested in a post the other day:

    http://communities.sas.com/message/104323#104323

    Frequent Contributor
    Posts: 89

    how to create new variables based on information from old variables?

    Thank you very much.The code you gave to me works perfectly for me

    By the way, I find another way that can also do the job. Here is the code (I am not sure if this method is considered professional or not):

    data _Null_;

    set old;

    array stringvar

  • _CHARACTER_;
  • NumofCVar = dim(stringvar) ;

    call SYMPUTX('NumOfCVar',NumOfCVar);

    run;

    data new;

    set old;

    array old_var_array

  • _CHARACTER_;
  • array _NewVar[&NumofCVar] NewVar1-NewVar&NumofCVar;

    ..............

    PROC Star
    Posts: 7,360

    how to create new variables based on information from old variables?

    Looks almost sufficiently professional enough to me for whatever that is worth.  However, I would simplify the first step so that you don't read through all of the records.  e.g.:

    data _Null_;

      set have;

      if _n_ eq 1 then do;

        array stringvar

  • _CHARACTER_;
  •     NumofCVar = dim(stringvar) ;

        call SYMPUTX('NumOfCVar',NumOfCVar);

      end;

      stop;

    run;

    Frequent Contributor
    Posts: 89

    how to create new variables based on information from old variables?

    thanks a lot for suggestion

    Super User
    Posts: 9,671

    how to create new variables based on information from old variables?

    dictionary tables already contains the number of character variables.

    proc sql ;

    select num_character

      from dictionary.tables

       where libname='SASHELP' and memname='CLASS';

    quit;

    Ksharp

    Frequent Contributor
    Posts: 89

    how to create new variables based on information from old variables?

    thank you Ksharp. I actually didn't know there exists "dictionary tables". It is such a wonderful tool.

    ☑ This topic is SOLVED.

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

    Discussion stats
    • 6 replies
    • 138 views
    • 6 likes
    • 3 in conversation