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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 5290 views
  • 4 likes
  • 5 in conversation