<?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: How do I create a variable from the value of another variable in a datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274149#M54665</link>
    <description>&lt;P&gt;You need to post examples of the data in and data wanted out. &amp;nbsp;For example what do two datasets (modelData and&amp;nbsp;states) you are loading into hash tables look like? &amp;nbsp;What does the dataset,&amp;nbsp;DALY_agregate, that is driving the data step look like? &amp;nbsp;What do you want the DALY1 dataset to look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post in the form of a data step that others can run. You can simplify to make it easier to post. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 31 May 2016 16:35:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2016-05-31T16:35:11Z</dc:date>
    <item>
      <title>How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274108#M54649</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to create a variable from the value of another variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data aaaaa;
   infile datalines delimiter='09'x; 
   length uuu$2 vvv$2;
   input uuu $ vvv $;
   datalines;
aa	gg
bb	hh
cc	ii
dd	jj
ee	kk
ff	ll
;

data bbbbb;
	set aaaaa;

	*create a variable from the value of uuu;
	&amp;lt;&amp;lt;value_of_uuu&amp;gt;&amp;gt; = 'something';

	/* (and this single line would produce something like this:
	aa='something' when _N_=1;
	bb='something' when _N_=2;
	cc='something' when _N_=3;
	etc. )*/

	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if this is possible.&lt;/P&gt;
&lt;P&gt;This may seem unusefull, it&amp;nbsp;is the simplification of my problem which has several do-loops inside the datastep.&lt;/P&gt;
&lt;P&gt;Let me know if I need to explain more, but I feel it would quickly overcomplicate the question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--&lt;/P&gt;
&lt;P&gt;SAS9.3, EG7.1, Windows7&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274108#M54649</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T15:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274114#M54650</link>
      <description>&lt;P&gt;Errm, you have read the SAS guide yes, and understand base SAS? &amp;nbsp;Its just you have more or less written the code but seem to question a fundemental. &amp;nbsp;Use conditional branching, either if then's or select;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  if _n_=1 then aa="";
  if _n_=2 then bb="";
...
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 May 2016 14:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274114#M54650</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-31T14:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274118#M54652</link>
      <description>&lt;P&gt;That's not it, maybe I wasn't clear, I will edit the question.&amp;nbsp;I know base SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here I don't want to write aa = '' I want to write something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;value_of_uuu = ''&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and that value_of_uuu would be replace by aa in the first datastep iteration&lt;/P&gt;
&lt;P&gt;then bb in the second&lt;/P&gt;
&lt;P&gt;then cc in the third.&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274118#M54652</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T15:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274123#M54655</link>
      <description>&lt;P&gt;I think you should post an example of your starting data and the desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It almost sounds like you may want to use an array but setting values based on position in a dataset ( using _n_) is a tad odd.&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274123#M54655</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-31T15:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274126#M54656</link>
      <description>&lt;P&gt;I agree with ballardw, it seems like a straightforward array application:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array varlist {3} aa bb cc;&lt;/P&gt;
&lt;P&gt;value_of_uu = varlist{_n_};&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are items to consider.&amp;nbsp; As with all arrays, the elements (aa, bb, and cc) must all be character (or must all be numeric).&amp;nbsp; And if _n_ would ever be greater than 3, the code will generate an error.&amp;nbsp; You can't let the index of the array (_n_) exceed the number of elements in the array (3 for this example).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274126#M54656</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-31T15:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274127#M54657</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;, the request is quite odd. &amp;nbsp;You can do it, quite easily, but I don't understand why you want to:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set aaaa end=last;
  if _n_=1 then call execute('data want;  set aaaa; ');
  call execute(cat(' ',strip(uu),'="',strip(vv),'";');
  if last then call execute(';run;');
run;&lt;/PRE&gt;
&lt;P&gt;This assumes all character. &amp;nbsp;Why though is the real question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Edit, for&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding﻿&lt;/a&gt;, you could modify mine above to handle numbers/characters, and check existence and such like - its one of the benefits of generated code over arrays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274127#M54657</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-31T15:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274131#M54659</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;deleted&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 16:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274131#M54659</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T16:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274134#M54661</link>
      <description>&lt;P&gt;I didn't see&amp;nbsp;you were call executing an entire datastep from the first data step&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Never done this. But I tried your code (with adaptations to work with previous examples)&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;data bbbbb;
	set aaaaa end=last;
	if _n_=1 then call execute('data want;  set aaaaa; ');
	call execute(cat(' ',strip(uuu),'="something";'));
	if last then call execute(';run;');
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I think it works, I need to check if I can incorporate this idea in my larger problem&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;NOTE: CALL EXECUTE generated line.&lt;BR /&gt;1 + data want; set aaaaa;&lt;BR /&gt;2 The SAS System 13:31 Tuesday, May 31, 2016&lt;/P&gt;
&lt;P&gt;2 + aa="something";&lt;BR /&gt;3 + bb="something";&lt;BR /&gt;4 + cc="something";&lt;BR /&gt;5 + dd="something";&lt;BR /&gt;6 + ee="something";&lt;BR /&gt;7 + ff="something";&lt;BR /&gt;8 + ;run;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 15:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274134#M54661</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T15:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274140#M54662</link>
      <description>&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;It wouldn't be practical in my situation because my datastep is hundreds of lines long and It would be a mess to write call execute at every line.&lt;/P&gt;
&lt;P&gt;But it's definitely a working solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe I will write another post with the whole problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 16:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274140#M54662</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T16:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274143#M54663</link>
      <description>&lt;P&gt;PROC TRANSPOSE can use one variable to have the name and another to have the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=aaaaa out=want ;
   id uuu;
   var vvv;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will create varables named AA, BB, ... with values 'gg','hh',....&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 16:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274143#M54663</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-31T16:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274146#M54664</link>
      <description>&lt;P&gt;I need it to be in a datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;because&amp;nbsp;i'm using hash tables and do loops&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro dummy();
data DALY1;
	*** set lengths ;
	length Germ $10 Category1 $50 Category2 $50 AgeGroupDALY $10 Gender $2 value 8 statesList$999;

	*** make link to hash tables ;
	if _n_=1 then do;

		***modelvalues ----------------;
		declare hash h1(dataset:'modelData');
		h1.definekey ('Germ', 'Category1', 'Category2', 'AgeGroupDALY', 'Gender') ;
		h1.definedata('Value');
		h1.definedone();
		call missing(Germ, Value, Category1, Category2);

		***states ---------------------;
		declare hash h2(dataset:'states');
		h2.definekey ('Germ') ;
		h2.definedata('statesList');
		h2.definedone();
		
	end;

	set DALY_agregate;

	put "°°°°° _n_=" _n_;


	***get statesList;
	rc2=h2.find(KEY:Germ);
	put "statesList=" statesList;
	
	***iterate statesList;
	do i=1 to countw(statesList);
		put "=== i=" i;

		***get state from statesList;
		state = scan(statesList,i);
		put "state=" state;

*&amp;gt;&amp;gt;&amp;gt; create variable with the value of state &amp;lt;&amp;lt;&amp;lt;-------------------------------------------------------- ;

		***iterate propertyList;
		put "propertyList=&amp;amp;propertyList";
		do j=1 to countw("&amp;amp;propertyList");
			put "- j=" j;

			***get property from &amp;amp;propertyList;
			property = scan("&amp;amp;propertyList",j);
			put "property=" property;

			
			***get value;
			put Germ "-" state "-" property "-" AgeGroupDALY "-" Gender;
			rc1=h1.find(KEY:Germ, KEY:state, KEY:property, KEY:AgeGroupDALY, KEY:Gender);
			put "value=" value;

			duration = 


		end; ***end iterate propertyList;

		
	end; ***end iterate statesList;

	***clean dataset;
	drop i j;




	***this is another attempt with macro variables;
	%do k=1 %to %sysfunc(countw(&amp;amp;statesList)); *e.g. acute_1 asymptomatic_1 ...;
		%let state = %scan(&amp;amp;statesList, &amp;amp;k);
		put "=== &amp;amp;k &amp;amp;state";
		&amp;amp;state = 1; * multiplication of terms ;


		put "propertyList=&amp;amp;propertyList";

		%do i=1 %to %sysfunc(countw(&amp;amp;propertyList)); *e.g. Severity Duration Probability;
			%let property = %scan(&amp;amp;propertyList, &amp;amp;i);
			put "-- &amp;amp;i &amp;amp;property";



			* set value with lookup ;
			rc=h1.find(KEY:Germ, KEY:"&amp;amp;state", KEY:"&amp;amp;property", KEY:AgeGroupDALY, KEY:Gender);
			
			* IF MISSING --&amp;gt; 1 , no weight *;
			if rc ^= 0 then value = 1;
			if value = . then value = 1;

			* IF DURATION = -1 --&amp;gt; take the SPMA Life Expectancy ;
			if index(LOWCASE("&amp;amp;property"),'duration') ge 1 then do;
				if value=-1 then do;
					value = input(put(cats(Gender,'_',AgeGroupSpma),Gender_AG2SPMA2013_.),10.5);
				end;
			end;

			* IF FATAL then take GBD2010;
			if index(LOWCASE("&amp;amp;state"),'fatal') ge 1 then do;
				if index(LOWCASE("&amp;amp;property"),'severity') ge 1 then do;
					value=1;
				end;
				if index(LOWCASE("&amp;amp;property"),'duration') ge 1 then do;
					*put 'YLL======================================';
					value = input(put(cats(Gender,'_',AgeGroupDALY),Gender_AG2GBD2010_.),10.5);
					*value = 99;
				end;
			end;

			&amp;amp;state._&amp;amp;property = value;
			&amp;amp;state = &amp;amp;state * value;
		%end;

	%end;

run;
%mend dummy;
%dummy;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 May 2016 16:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274146#M54664</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T16:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274149#M54665</link>
      <description>&lt;P&gt;You need to post examples of the data in and data wanted out. &amp;nbsp;For example what do two datasets (modelData and&amp;nbsp;states) you are loading into hash tables look like? &amp;nbsp;What does the dataset,&amp;nbsp;DALY_agregate, that is driving the data step look like? &amp;nbsp;What do you want the DALY1 dataset to look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post in the form of a data step that others can run. You can simplify to make it easier to post. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 16:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274149#M54665</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-31T16:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a variable from the value of another variable in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274152#M54667</link>
      <description>I will put everything together and ask in another thread in a few weeks.&lt;BR /&gt;Thanks!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Tue, 31 May 2016 16:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-variable-from-the-value-of-another-variable-in/m-p/274152#M54667</guid>
      <dc:creator>mathias</dc:creator>
      <dc:date>2016-05-31T16:38:54Z</dc:date>
    </item>
  </channel>
</rss>

