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

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.

    1 ACCEPTED SOLUTION

    Accepted Solutions
    6 REPLIES 6
    art297
    Opal | Level 21

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

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

    littlestone
    Fluorite | Level 6

    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;

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

    art297
    Opal | Level 21

    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;

    littlestone
    Fluorite | Level 6

    thanks a lot for suggestion

    Ksharp
    Super User

    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

    littlestone
    Fluorite | Level 6

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

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 6 replies
    • 810 views
    • 6 likes
    • 3 in conversation