- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I would like to create the dataset in the attachment from scratch using SAS codes.
For 1st column, I would like to create a sequence of unique numbers in a column, but each number must repeat 10 times, for example, number 1 in the first 10 rows, number 2 in the next 10 rows, and so on. I have tried the following code and it worked.
data a; call streaminit(123); do i = 1 to 10; j=1; do while (j<11); id=i; output; j+1; end; end; drop i j; run;
For 2nd column, I would like to create a sequence of dates that are between 2008/01/01 and 2008/01/11, with replacement, and repeat that sequence 10 times.
For 3rd column, I would like to generate 100 random numbers (with 2 decimal points) between 30 and 50.
I would appreciate all the help there is. I need help on creating column 2 and 3. Thanks in advance!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@stevenyan0127 wrote:
Yes, you are correct. The dates are not with replacement within an ID. Do you know how to create that repeated sequence of dates?
Likely the easiest is to 1) create the sequence 2) include a random value for order and 3) sort the data by id and the order random value. The dates will be there in random order.
data a; call streaminit(123); do id = 1 to 10; do randdate = '01Jan2008'd to '11Jan2008'd ; ordervar = rand('uniform'); randnum = round( 30 + 20*rand('uniform'), 0.01); output; end; end; format randdate yymmddd10.; run; proc sort data=a out=want(drop=ordervar); by id ordervar; run;
I simplified that do while as not needed for this case. If you were generating a random number of values for each id then something like that would be needed, maybe.
You statement ID=i; is a clue that perhaps instead of i=1 to 10 that id=1 to 10 would suffice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It looks like your rule for the dates is not with replacement within an ID. If that is so you need to say so.
This will generate a random number for 30 to 50:
randnum = round( 30 + 20*rand('uniform'), 0.01);
Basically generating a random from 0 to 20 and adding that to 30 with the whole result rounded to 2 decimal places.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@stevenyan0127 wrote:
Yes, you are correct. The dates are not with replacement within an ID. Do you know how to create that repeated sequence of dates?
Likely the easiest is to 1) create the sequence 2) include a random value for order and 3) sort the data by id and the order random value. The dates will be there in random order.
data a; call streaminit(123); do id = 1 to 10; do randdate = '01Jan2008'd to '11Jan2008'd ; ordervar = rand('uniform'); randnum = round( 30 + 20*rand('uniform'), 0.01); output; end; end; format randdate yymmddd10.; run; proc sort data=a out=want(drop=ordervar); by id ordervar; run;
I simplified that do while as not needed for this case. If you were generating a random number of values for each id then something like that would be needed, maybe.
You statement ID=i; is a clue that perhaps instead of i=1 to 10 that id=1 to 10 would suffice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I have the following R codes where I'm randomly generating multi level categorical variables in (1), and in (2) generating numbers and dates. My question is how to write SAS codes that are equivalent to these R codes. I would appreciate all the help there is. Thanks in advance!
1. set.seed(5)
data <- data.frame(Pat_ID <- 1:10)
data1 <- data %>%
add_column(Name = sample(c("A","B","C",""),10,replace=T))%>%
rename(Pat_ID = Pat_ID....1.10)
2. set.seed(3)
ID <- c(rep(1:10,each=10))
Date <- rep(sample(seq(as.Date('2008/01/01'), as.Date('2008/01/11'), by="day",replace=T), 10),10)
Cost <- round(runif(100,30,50),digits = 2)
df <- data.frame(ID,Date,Cost,check.names = F)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think the RAND function does both.
x = rand('uniform',100,200);
x = rand('integer',1,10);
There are plenty of other distributions if you want in the RAND function documentation. https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p0fpeei0opypg8n1b06qe4r040lv.htm Also see the section "Reproducing a Random Number Stream".
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Describe how you want the random values generate, distribution and such. I don't speak R well enough to decipher such (and I'm not at all interested in trying to dig through a bunch of packages to try to find out).
The SAS function Rand has about 20 different types of random numbers to generate. Most are distributions.
Dates would probably respond to Rand('integer',a,b) which generates integers in the interval specified by a and b. SAS Dates are integer number of days so if you want to create values between two dates such as 1 Jan 2000 and 15 Jan 2000:
randdate = rand('integer', '01Jan2000'd, '15Jan2000'd);
If you have not used SAS date literals they must be as a DATE7. Date9 or similar form shown: 'ddMONyyyy'd, in quotes with the D immediately following so SAS knows you intend to use the Date and not a character value. You will want to apply a format to the result for people to read.
If you want a character result then either use an ARRAY with the values set and select a random index, or assign a custom format to an integer result.
As I said, I cannot tell even which calls are "random" in your code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Would you mind take a look? Thanks!