04-30-2014 11:18 AM
I have a data set that includes a number of buildings, with multiple records for each building. I want to generate random 5-digit ID for each building, and I want the same ID to apply to each observation from a given building.
I was able to generate IDs for the buildings, like so:
if building^=lag(building) then BuildingID+1;
However, this gives me IDs that start with 1 and progress consecutively. I really want the IDs to be randomly generated, 5-digit numbers. I can't see how to do this while maintaining the same ID across multiple records for a given building.
Any assistance is appreciated.
04-30-2014 11:41 AM
bnum = put( round(100000*Ranuni(327),1),z5.);
Will get you close, though you need to check to see if any duplicates are created in the BNUM.
Since I don't think an identifier variable should have arithmetic performed on it I don't think there should be a problem with a character variable.
Or if you really want a numeric, take out the put, though some of the results will be fewer than 5 digits.
04-30-2014 11:54 AM
Thank you for this suggestion. I tried it, and it did generate random 5 digit numbers. However, it did not maintain the same buildingID across multiple records from the same building.
Here, in essence, is what I got:
Here is what I need:
04-30-2014 12:10 PM
Get the uniquie buildings and assign the number.
Then merge the data.
Note: New will be in building order when finished. If for some reason you need to keep an existing order you may need an order variable OR the create table can have an Order clause after the From to sort the data: order by var1, var2, var3 as needed.
create table uniquebldg as
select distinct building, put( round(100000*Ranuni(327),1),z5.) as bnum
Create table new as
select a.*, b.bnum
from old as a left join uniquebldg as b
04-30-2014 12:16 PM
Here is one way to insure you have random list of five digit numbers from 10000 to 99999. Then to apply that to your data get(set) one from the list when FIRST.BUILDINGNAME. You may need to sort your building data first if the buildings are not grouped.
04-30-2014 12:18 PM
Get a distinct list of building IDs.
Generate a set of random IDs that meet your criteria.
Create a format out of that
Apply the format in dataset to get your new ID.
create table distinct_id as
select distinct buildingID, put( round(100000*Ranuni(327),1),z5.) as randomID from have as A;
*create your format;
05-01-2014 05:34 AM
if first.buildingname then bnum = put( round(100000*Ranuni(327),1),z5.);
Or another function:
data x; length uuid $ 5; do i=1 to 100; uuid=compress(uuidgen(), ,'kd'); output; end; run;