Hi, I have a question about how to assign random variables to a dataset.
For example, I have a dataset USCRE and a random seed sets, they are:
PAY_FREQ_CD = {QUA, MON, ANN}
ECL_AMZ_TP_CD ={BULLET, DDLNR, CCF}
For my dataset USCRE, how can I randomly pick a value from PAY_FREQ_CD & ECL_AMZ_TP_CD for each of the record in my dataset USCRE? Suppose the number of records are unknown here.
And there is another variable ECL_AMZ_TP_DSC, it should take the same value as ECL_AMZ_TP_CD, what should I do?
Thank you!
Lydia
It helps to provide some actual example starting data and what a typical result might look like.
I don't know what you mean by
PAY_FREQ_CD = {QUA, MON, ANN}
ECL_AMZ_TP_CD ={BULLET, DDLNR, CCF}
Does that mean the variable PAY_FREQ_CD has values? Or are you indicating that those are the Possible values that you want assigned? Are those values supposed to represent the name of another variable or litteral text for a result?
And why would the number of records have any bearing? If the object is to randomly select a value from list why would the record matter?
Are the probabilities of selecting the values supposed to be the same or is some more likely than others?
If you know how many objects are in a potential list AND if you want equal probability of selection you can use the Rand function with the 'integer' type of selection.
Data want;
set have;
itemchoice = rand('integer',3);
run;
will add a variable named itemchoice and take values of 1,2 or 3 with 1/3 chance of selection.
If your QUA, MON, ANN are the names of variables in your data set then perhaps;
data want; set have; array dd {*} Qua mon ann; array ee {*} bullet ddlnr ccf; Pay_freq_cd = dd[ rand('integer',3)]; Ecl_amz_tp_cd = ee[rand('integer',3)]; Ecl_amz_tp_dsc=Ecl_amz_tp_cd; run;
If you want TEXT value of qua mon or ann
data want; set have; dd="QUA MON ANN"; ee="BULLET DDLNR CCF"; Pay_freq_cd = scan(dd,rand('integer',3)); Ecl_amz_tp_cd = scan(ee,rand('integer',3)); Ecl_amz_tp_dsc=Ecl_amz_tp_cd; run;
PAY_FREQ_CD = {QUA, MON, ANN}
ECL_AMZ_TP_CD ={BULLET, DDLNR, CCF}
This means for the variable PAY_FREQ_CD, it could possibly be QUA or MON or ANN.
Same for ECL_AMZ_TP_CD, the ECL_AMZ_TP_CD could be assigned with BULLET or DDLNR or CCF.
Thanks,
Lydia
@y658li wrote:
and QUA, MON, ANN those are values stand for quarterly, monthly and annully.
Actually doesn't quite answer the question of are they Variables or text value to assign.
Examples help. Did you try either or both of the possible solutions?
Yeah I tried both solutions and first one has error message:
ERROR: Illegal reference to the array dd.
ERROR: Illegal reference to the array ee.
The second solution is not what I want.
What I want is shown as this:
For each of record, PAY_FREQ_CD select either QUA or MON or ANN
ECL_AMZ_TP_CD select either CFF or BULLET or DDLNR, each value has 1/3 chance to be selected.
Thanks,
Lydia
First, when ever you get an error then from the log copy the entire data step or procedure with all the messages and notes then paste into a text box on the forum opened with the </> icon. The text box is important to preserve the diagnostic characters in the correct positions that SAS often places in the log. The message windows will reformat them.
And that looks exactly like what the second solution does.
(editted to create 10 records and drop out some variables)
data want; dd="QUA MON ANN"; ee="BULLET DDLNR CCF"; do record=1 to 10; Pay_freq_cd = scan(dd,rand('integer',3)); Ecl_amz_tp_cd = scan(ee,rand('integer',3)); Ecl_amz_tp_dsc=Ecl_amz_tp_cd; output; end; drop dd ee; run;
Generates:
Pay_ Ecl_amz_ Ecl_amz_ record freq_cd tp_cd tp_dsc 1 MON BULLET BULLET 2 ANN DDLNR DDLNR 3 QUA CCF CCF 4 ANN BULLET BULLET 5 MON DDLNR DDLNR 6 MON DDLNR DDLNR 7 MON DDLNR DDLNR 8 MON DDLNR DDLNR 9 ANN CCF CCF 10 MON CCF CCF
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.