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

Hi ,
I create this data set:

data temp;
 do i=1 to 5;
    a1=999;
    a2=999;
    a3=999;
    ...
   a10 =999;
   output;
 end;
run;


is there a better solution that create a second do-loop inside the first do-loop?

for example like this:

data temp;
 do i=1 to 10;
  do j=1 to 10;
call symput("index",j);
a&index=999; end;
output;
end; run;

The last example, doesn't work, because it doesn't recognize the index variable.

Is there some solution?

 

1 ACCEPTED SOLUTION
5 REPLIES 5
Astounding
PROC Star

SInce your new variables are constant, a better solution would not nest loops:

 

data want;

retain a1-a10 999;

do i=1 to 5;

   output;

end;

run;

 

To answer your original question, no there is no solution.  SAS does not let you change what "a&index" resolves to, as the DATA step executes.

Rakeon
Quartz | Level 8

thanks!!!

Reeza
Super User

@Rakeon please mark the question as answered.

rogerjdeangelis
Barite | Level 11
A slightly more interesting problem;

Generating 10 observations with a random number of variables

Minimum number of variables is 1(a1) max is 5(a1-a5)

This is trivial in IML or R.

  n <- as.integer(5*runif(1)) +1;
  runif(n);

> n <- as.integer(5*runif(1)) +1;
  runif(n);
[1] 0.13442659 0.07485726 0.22734428 0.84948492
>

HAVE a random number between 1 and 5
========================================

    n=int(5*uniform(5739)) + 1;

  Note N could be meta data

WANT  Suppose the random n is 3
===============================

Up to 40 obs from want total obs=10

Obs       A1         A2         A3

  1    0.39041    0.51861    0.19275
  2    0.13243    0.54758    0.86705
  3    0.89960    0.18865    0.46915
  4    0.62604    0.15337    0.34970
  5    0.50958    0.16918    0.61778
  6    0.81300    0.88339    0.36657
  7    0.79296    0.56760    0.66014
  8    0.37525    0.19073    0.66443
  9    0.70009    0.31034    0.72677
 10    0.56065    0.93989    0.41765

SOLUTION
========

data _null_;
  n=int(5*uniform(5739)) + 1;
  call symputx('dim',put(n,3.));
   rc=dosubl('
      data want(drop=rec i);
        array a[&dim.] a1-a&dim;
        do rec=1 to 10;
           do i=1 to &dim;
              a[i]=uniform(5733);
           end;
           output;
        end;
      run;quit;
   ');
  stop;
run;quit;

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!

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
  • 5 replies
  • 4324 views
  • 4 likes
  • 5 in conversation