how to create loop from a random or choosen number

Reply
Contributor
Posts: 26

how to create loop from a random or choosen number

Dear all,

          If i,ve a dataset containing one variable with the obsevations 1-20 (say) from this dataset 2 subjects withdrawn 9, 16 (say) and i've choosen a random no. (say 18), now i need the output starting from 18 to 20% of data(when reached end of obs. then start from first like 18,20,1,2)

               thanks in advance.

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: how to create loop from a random or choosen number

Sorry, I can't follow your logic.  Post have data (in terms of a datastep) and required output.  If you want loops then look at do loops. 

Respected Advisor
Posts: 3,831

Re: how to create loop from a random or choosen number

I don't understand how the first part of your question relates to the second part.

Below a code sample for the second part of your question how I understand it.

data have;

  do var=1 to 20;

    output;

  end;

  stop;

run;

data select(drop=_Smiley Happy;

  _start=ceil(ranuni(0)*_nobs);

  do _i=_start to _nobs;

      _counter+1;

    if _counter>4 then leave;

    set have nobs=_nobs point=_i;

    if _i=_nobs then _i=0;

    output;

  end;

  stop;

run;

Contributor
Posts: 26

Re: how to create loop from a random or choosen number

thanks, it could be very helpful.

Contributor
Posts: 26

Re: how to create loop from a random or choosen number

Giving decimal places and significant digits in SAS


Dear all,

i've a dataset like given bellow

                

ABCD
N363636
Mean9581.17289791.52621366.1106
SD2965.86262982.0749 398.3543
Geo_Mean9170.2867 2.0864674 1312.3213
CV13.95511 13.455669 0.159741

 

Now i need to give 3 decimal places for the variables 'B' & 'C', and 5 significant digits for the variable 'D'. But these decimal places and significant digits should not be applyed for observations of 'N'. i had tried many things like format, length and etc. but it didn't work well. the output should be like given bellow.


ABCD
N363636
Mean9581.1729791.5261366.1
SD2965.8622982.074398.35
Geo_Mean9170.2862.086 1312.3
CV13.955 13.4550.15974


i'm excited to find is it really possible? if so, can anyone tell me how? or it is possible by transforming the variables?

Thanks in advance

Grand Advisor
Posts: 9,576

Re: how to create loop from a random or choosen number

The most direct way is changing these variables into character variables .

The alternative way is using proc format or proc report + call define().

Xia Keshan

Grand Advisor
Posts: 9,576

Re: how to create loop from a random or choosen number

data have;

  do var=1 to 20;

    output;

  end;

  stop;

run;

data select;

array x{0:999999} _temporary_;

n=-1;

do until(last);

  set have end=last;

  n+1;x{n}=var;

end;

do i=18 to 21;

  want=x{mod(i,20)}; output;

end;

keep want;

stop;

run;

Xia Keshan

Ask a Question
Discussion stats
  • 6 replies
  • 2128 views
  • 1 like
  • 4 in conversation