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

The following, except for the fourth line from the bottom of the code, does what I want.  However, in the section:

  do _i=1 to 4;

    want((_n_-1)*4+_i)=scan("&vars.",_i);

  end;

I'd like to assign the values of var1, var2, var3 and var4

What do I have to change/add/modify in order to obtain that result?

TIA,

Art

data have (drop=months);

  format id best32.;

  input date (ind1-ind4) ($) ;

  informat date date9.;

  format date yymon7.;

  array other_variable(2);

  do id=1 to 2;

    other_variable(id)=id;

  end;

  other_variable1=1;

  other_variable2=2;

  do id=1 to 10;

    date="01dec2010"d;

    do months=1 to 12;

      date=intnx('month',date,1);

      if not(months eq 9 and mod(id,2)) then output;

    end;

  end;

  cards;

01dec2010 A B C D

;

%let filein=have;

%let by_variable=id;

%let vars=ind1 ind2 ind3 ind4;

%let numvars=4;

%let id_label=date;

proc sql noprint;

  select distinct 'ind1_'||put(date,yymon7.)||

                  ' ind2_'||put(date,yymon7.)||

                  ' ind3_'||put(date,yymon7.)||

                  ' ind4_'||put(date,yymon7.),

                  date

    into :varlist separated by " ",

         :junk

      from &filein. (obs=1000) /*must be big enough to capture all months*/

        order by &id_label.

  ;

quit;

data want;

  set &filein.;

  by &by_variable.;

  array want(*) $ &varlist.;

  retain want;

  if first.id then call missing(of want(*));

  _n_=month(date);

  do _i=1 to 4;

    want((_n_-1)*4+_i)=scan("&vars.",_i);

  end;

  drop &id_label. _: &vars.;

  if last.&by_variable. then output;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.

want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));


View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

You need another ARRAY.

array want(*) $ &varlist.;

array have $ &vars ;

   ...

want((_n_-1)*4+_i)=have(_i) ;

art297
Opal | Level 21

Tom,  I'll come back and mark your response as "correct" rather than just "helpful" if no one can come up with a way of doing what I'd really like to do.  However, using an array definitely solved the problem.

Tom
Super User Tom
Super User

Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.

want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));


art297
Opal | Level 21

: That was definitely what I was looking for and, yes, it would allow combining character and numeric variables if I changed the program so that it wasn't forcing all of the results into a single array.

However, I'm not going to use it, after all, as it came with a high price.  It doubled the cpu time required for the overall process.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 1614 views
  • 4 likes
  • 2 in conversation