<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: replace every value of a variable with another randomly selected variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768677#M243828</link>
    <description>1. Do you want consistency across ID's or names? Ie does Mark map to same name each time or does ID 4 map to the same name each time? In that case you'll need some unique identifier per individual. &lt;BR /&gt;2. Do you need to be able to recreate this in any fashion? If you re-run it later are you expected to match the data?&lt;BR /&gt;3. What do you want the output to look like?</description>
    <pubDate>Mon, 20 Sep 2021 22:45:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-09-20T22:45:31Z</dc:date>
    <item>
      <title>replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768672#M243824</link>
      <description>&lt;P&gt;I am an experienced SAS coder but apparently I am very dusty and need some quick help. I've searched online and the community forums without finding a tidy solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with 1000s of variable values (names) that I need to replace with randomly selected names from a second table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The number of rows in each table is different.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset CLAIMS has a variable called firstname.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset FIRSTNAMES contains a variable (unique list) called newname.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to replace each value of firstname with a random selection from newname.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone provide some pseudocode that would accomplish this relatively easily?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 21:44:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768672#M243824</guid>
      <dc:creator>Fugue</dc:creator>
      <dc:date>2021-09-20T21:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768674#M243826</link>
      <description>Can you please quickly make some fake data that follows the structure of your input files and provide that as a starting point?</description>
      <pubDate>Mon, 20 Sep 2021 21:47:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768674#M243826</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-20T21:47:20Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768675#M243827</link>
      <description>&lt;P&gt;The attached workbooks should give an idea of the structure of the data. Assume these are SAS datasets, of course.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 22:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768675#M243827</guid>
      <dc:creator>Fugue</dc:creator>
      <dc:date>2021-09-20T22:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768677#M243828</link>
      <description>1. Do you want consistency across ID's or names? Ie does Mark map to same name each time or does ID 4 map to the same name each time? In that case you'll need some unique identifier per individual. &lt;BR /&gt;2. Do you need to be able to recreate this in any fashion? If you re-run it later are you expected to match the data?&lt;BR /&gt;3. What do you want the output to look like?</description>
      <pubDate>Mon, 20 Sep 2021 22:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768677#M243828</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-20T22:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768678#M243829</link>
      <description>Here's an outline of how to map an ID to a random number: &lt;A href="https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644" target="_blank"&gt;https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Not sure if you solved your issue since you marked my answer, if not you should unmark it. &lt;BR /&gt;Once you answer the questions, it should be fairly straightforward depending on the answers. &lt;BR /&gt;</description>
      <pubDate>Mon, 20 Sep 2021 22:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768678#M243829</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-20T22:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768903#M243904</link>
      <description>&lt;P&gt;Thanks, Reeza. I will take a look at that link.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did not mark any of your replies. Seems the forum is doing that all on its own!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As to your earlier questions: I do not need replicability. Nor do I need consistent name matching. Just need to replace actual names with random names. Random names can be reused "infinitely".&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 17:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768903#M243904</guid>
      <dc:creator>Fugue</dc:creator>
      <dc:date>2021-09-21T17:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768908#M243907</link>
      <description>&lt;P&gt;Load all your new names into a temporary array, and then use rand("integer",1,dim(array)) to get a random index.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 18:13:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768908#M243907</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-21T18:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768918#M243912</link>
      <description>&lt;P&gt;Building off this example here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/2f733d27820f43fa37d6ba92c30f22cf" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/2f733d27820f43fa37d6ba92c30f22cf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; suggestion you can randomly assign a new name as follows then. This was originally designed to search terms but re-assignment is a similar concept so it will work just as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Make fake data to show example;
*terms to search for;
data terms; 
set sashelp.baseball (obs=5);
search_term = substr(team,1,3);
keep search_term;;
run;
    
*main data set that will be searched;
data test; 
set sashelp.baseball;
run;


/*General process to the solution*/
******************************************************************************************
1. Store the number of terms in a macro variable to assign the length of arrays
2. Load terms to search into a temporary array
3. Loop through for each word and search the terms
4. Exit loop if you find the term to help speed up the process
******************************************************************************************;



/*1*/
proc sql noprint;
select count(*) into :num_search_terms from terms;
quit;

%put &amp;amp;num_search_terms.;



data flagged;
*for reproducibility while testing;
call streaminit(456);

*declare array;
array _replace(&amp;amp;num_search_terms.) $ _temporary_;

/*2*/
*load array into memory;
   if _n_ = 1 then do j=1 to &amp;amp;num_search_terms.;
   set terms;
   _replace(j) = search_term;
   end;
 
 set test;
 
 *set flag to 0 for initial start;
 flag = 0;

/*3*/
*Replaces name with random one generated;
random_name = _replace(rand('integer', 1, &amp;amp;num_search_terms));

drop i j search_term ;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30806"&gt;@Fugue&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, Reeza. I will take a look at that link.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did not mark any of your replies. Seems the forum is doing that all on its own!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to your earlier questions: I do not need replicability. Nor do I need consistent name matching. Just need to replace actual names with random names. Random names can be reused "infinitely".&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 19:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/768918#M243912</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-21T19:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/769021#M243953</link>
      <description>&lt;P&gt;See example code for my suggestion, applying the logic to the SASHELP.CLASS dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data firstnames;
input name $;
datalines;
Charles
Diana
William
Harry
George
;

proc sql noprint;
select nobs into :nobs from dictionary.tables
where libname = "WORK" and memname = "FIRSTNAMES";
quit;

data want;
array names {&amp;amp;nobs.} $ _temporary_;
if _n_ = 1
then do i = 1 to &amp;amp;nobs.;
  set firstnames;
  names{i} = name;
end;
set sashelp.class;
name = names{rand('integer',1,&amp;amp;nobs.)};
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;EM&gt;Edit: fixed a missing m in the SQL (fro&lt;FONT color="#FF0000"&gt;m&lt;/FONT&gt;)&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 07:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/769021#M243953</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-22T07:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: replace every value of a variable with another randomly selected variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/769732#M244092</link>
      <description>&lt;P&gt;Hi Reeza. That example would work. I did not try it out as I was under a time crunch. But, thank you for the link and the info. I appreciate the time you've taken.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thought you should know that the forum keeps marking some reply as the "solution" without me doing anything at all. Not sure why the forum is doing that.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 21:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-every-value-of-a-variable-with-another-randomly-selected/m-p/769732#M244092</guid>
      <dc:creator>Fugue</dc:creator>
      <dc:date>2021-09-22T21:34:11Z</dc:date>
    </item>
  </channel>
</rss>

