<?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: Create new variables using and based on arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778473#M247787</link>
    <description>&lt;P&gt;You cannot dynamically create new variables in a data step. The code is compiled once.&lt;/P&gt;
&lt;P&gt;So you will have to get the names of the variables you need first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Give data like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;      
  input x y z p;
cards;          
1 5 2 65        
46 1 43 65      
2 3 4 1         
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can get the names of the new variables that you want into a macro variable like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;                                               
  set have;                                                
  length newnames $2000;                                   
  array modify(*) x--p;                                    
  do _N_=1 to dim(modify);                                 
    call catx(' ',newnames,cats(vname(modify(_N_)),'_r')); 
    end;                                                   
  call symputx('newvars',newnames);                        
  stop;                                                    
run;                                                       
%put &amp;amp;newvars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then it is relatively simple to do the rest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                    
  set have;                   
  array modify(*) x--p;       
  array newvars(*) 8 &amp;amp;newvars;
  do _N_=1 to dim(modify);    
    if modify(_N_)=1 then     
      newvars(_N_)=5;         
    end;                      
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 04 Nov 2021 11:48:48 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2021-11-04T11:48:48Z</dc:date>
    <item>
      <title>Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778448#M247775</link>
      <description>&lt;P&gt;Dear SAS experts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create new variables which are modified versions of existing variables. I would like the new variables to have a very similar names to those already present. Adding a simple suffix, e.g. "_r", would be fine. Because I want to perform this operation many times (many variables, all numerical) I would like to do it using an array code. However, I cannot get it to work (desired variable: want_var). Here is a very simple example with only one variable of what I am trying to accomplish. Can someone suggest a modification to the array code? Thanks. I realize that the array code is not logical for only variable, but it is just meant as an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input have_var want_var;&lt;BR /&gt;datalines;&lt;BR /&gt;. .&lt;BR /&gt;1 5 &lt;BR /&gt;2 .&lt;BR /&gt;8 .&lt;BR /&gt;3 . &lt;BR /&gt;. .&lt;BR /&gt;7 .&lt;BR /&gt;2 .&lt;BR /&gt;1 5&lt;BR /&gt;. .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have (drop=i);&lt;BR /&gt;set have;&lt;BR /&gt;array modify have_var;&lt;BR /&gt;do i=1 to dim(modify);&lt;BR /&gt;if modify(i)=1 then modify(i)_r=5;&lt;BR /&gt;end;&lt;BR /&gt;run;&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;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 ;*';*";*/;quit;run;&lt;BR /&gt;2 OPTIONS PAGENO=MIN;&lt;BR /&gt;3 %LET _CLIENTTASKLABEL='Program';&lt;BR /&gt;4 %LET _CLIENTPROCESSFLOWNAME='Process Flow';&lt;BR /&gt;5 %LET _CLIENTPROJECTPATH='';&lt;BR /&gt;6 %LET _CLIENTPROJECTPATHHOST='';&lt;BR /&gt;7 %LET _CLIENTPROJECTNAME='';&lt;BR /&gt;8 %LET _SASPROGRAMFILE='';&lt;BR /&gt;9 %LET _SASPROGRAMFILEHOST='';&lt;BR /&gt;10 &lt;BR /&gt;11 ODS _ALL_ CLOSE;&lt;BR /&gt;12 OPTIONS DEV=PNG;&lt;BR /&gt;13 GOPTIONS XPIXELS=0 YPIXELS=0;&lt;BR /&gt;14 FILENAME EGSR TEMP;&lt;BR /&gt;15 ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR&lt;BR /&gt;16 STYLE=HtmlBlue&lt;BR /&gt;17 STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/SASEnterpriseGuide/7.1/Styles/HtmlBlue.css")&lt;BR /&gt;18 NOGTITLE&lt;BR /&gt;19 NOGFOOTNOTE&lt;BR /&gt;20 GPATH=&amp;amp;sasworklocation&lt;BR /&gt;SYMBOLGEN: Macro variable SASWORKLOCATION resolves to "E:\Work\_TD158488_SRVESBAPPSAS34V_\Prc2/"&lt;BR /&gt;21 ENCODING=UTF8&lt;BR /&gt;22 options(rolap="on")&lt;BR /&gt;23 ;&lt;BR /&gt;NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR&lt;BR /&gt;24 &lt;BR /&gt;25 GOPTIONS ACCESSIBLE;&lt;BR /&gt;26 data have (drop=i);&lt;BR /&gt;27 set have;&lt;BR /&gt;28 array modify have_var;&lt;BR /&gt;29 do i=1 to dim(modify);&lt;BR /&gt;30 if modify(i)=1 then modify(i)_r=5;&lt;BR /&gt;__&lt;BR /&gt;22&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: +, =.&lt;/P&gt;
&lt;P&gt;31 end;&lt;BR /&gt;32 run;&lt;/P&gt;
&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;MVA_DSIO.OPEN_CLOSE| _DISARM| STOP| _DISARM| 2021-11-04T10:22:54,259+01:00| _DISARM| WorkspaceServer| _DISARM| | _DISARM| | &lt;BR /&gt;_DISARM| | _DISARM| 21860352| _DISARM| 10| _DISARM| 18| _DISARM| 3473| _DISARM| 9042502| _DISARM| | _DISARM| | _DISARM| | _DISARM| &lt;BR /&gt;| _DISARM| | _DISARM| | _ENDDISARM &lt;BR /&gt;WARNING: The data set WORK.HAVE may be incomplete. When this step was stopped there were 0 observations and 3 variables.&lt;BR /&gt;MVA_DSIO.OPEN_CLOSE| _DISARM| STOP| _DISARM| 2021-11-04T10:22:54,259+01:00| _DISARM| WorkspaceServer| _DISARM| | _DISARM| | &lt;BR /&gt;_DISARM| | _DISARM| 21860352| _DISARM| 10| _DISARM| 18| _DISARM| 3997| _DISARM| 9045246| _DISARM| | _DISARM| | _DISARM| | _DISARM| &lt;BR /&gt;| _DISARM| | _DISARM| | _ENDDISARM &lt;BR /&gt;WARNING: Data set WORK.HAVE was not replaced because this step was stopped.&lt;BR /&gt;PROCEDURE| _DISARM| STOP| _DISARM| 2021-11-04T10:22:54,259+01:00| _DISARM| WorkspaceServer| _DISARM| | _DISARM| | _DISARM| &lt;BR /&gt;| _DISARM| 21860352| _DISARM| 10| _DISARM| 18| _DISARM| 140597| _DISARM| 9047432| _DISARM| | _DISARM| | _DISARM| | _DISARM| | &lt;BR /&gt;_DISARM| | _DISARM| | _ENDDISARM &lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;
&lt;P&gt;33 &lt;BR /&gt;34 GOPTIONS NOACCESSIBLE;&lt;BR /&gt;2 The SAS System 10:22 Thursday, November 4, 2021&lt;/P&gt;
&lt;P&gt;35 %LET _CLIENTTASKLABEL=;&lt;BR /&gt;36 %LET _CLIENTPROCESSFLOWNAME=;&lt;BR /&gt;37 %LET _CLIENTPROJECTPATH=;&lt;BR /&gt;38 %LET _CLIENTPROJECTPATHHOST=;&lt;BR /&gt;39 %LET _CLIENTPROJECTNAME=;&lt;BR /&gt;40 %LET _SASPROGRAMFILE=;&lt;BR /&gt;41 %LET _SASPROGRAMFILEHOST=;&lt;BR /&gt;42 &lt;BR /&gt;43 ;*';*";*/;quit;run;&lt;BR /&gt;44 ODS _ALL_ CLOSE;&lt;BR /&gt;45 &lt;BR /&gt;46 &lt;BR /&gt;47 QUIT; RUN;&lt;BR /&gt;48&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778448#M247775</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-11-04T09:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778450#M247776</link>
      <description>&lt;P&gt;Transpose to long, create the new variable, transpose back to wide, and merge with the existing dataset (unless you find that the long layout will be more useful anyway).&lt;/P&gt;
&lt;P&gt;See this example code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ var1 var2;
datalines;
X 1 2
Y 4 5
;

proc transpose
  data=have
  out=long (rename=(col1=var))
;
by id;
var var:;
run;

data want_long;
set long;
var_r = var * 5; /* or whatever you intend to do */
run;

proc transpose
  data=want_long
  out=want_wide (drop=_name_)
  prefix=var_r
;
by id;
var var_r;
run;

data want;
merge
  have
  want_wide
;
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778450#M247776</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-04T09:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778451#M247777</link>
      <description>&lt;P&gt;Dear Kurt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks. I was hoping it could be done in a more simple way though.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778451#M247777</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-11-04T09:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778452#M247778</link>
      <description>&lt;P&gt;To create new variables dependent on the number of variables already existing, you have to first determine the number, as this needs to be known before the data step is compiled. Arrays can't be used, as the DIMI() function is a runtime function.&lt;/P&gt;
&lt;P&gt;An alternative would be to read the variables from dictionary.columns and create the data step code from that, either with CALL EXECUTE or writing to an include file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And, as always, whenever there arises the need to work with a series of variables, contemplate switching to a long layout, which is always easier to code for.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 10:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778452#M247778</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-04T10:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778473#M247787</link>
      <description>&lt;P&gt;You cannot dynamically create new variables in a data step. The code is compiled once.&lt;/P&gt;
&lt;P&gt;So you will have to get the names of the variables you need first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Give data like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;      
  input x y z p;
cards;          
1 5 2 65        
46 1 43 65      
2 3 4 1         
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can get the names of the new variables that you want into a macro variable like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;                                               
  set have;                                                
  length newnames $2000;                                   
  array modify(*) x--p;                                    
  do _N_=1 to dim(modify);                                 
    call catx(' ',newnames,cats(vname(modify(_N_)),'_r')); 
    end;                                                   
  call symputx('newvars',newnames);                        
  stop;                                                    
run;                                                       
%put &amp;amp;newvars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then it is relatively simple to do the rest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                    
  set have;                   
  array modify(*) x--p;       
  array newvars(*) 8 &amp;amp;newvars;
  do _N_=1 to dim(modify);    
    if modify(_N_)=1 then     
      newvars(_N_)=5;         
    end;                      
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Nov 2021 11:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778473#M247787</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-11-04T11:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables using and based on arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778476#M247790</link>
      <description>&lt;P&gt;I'm not certain I understand the problem fully, but if you just want to do something like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
    myVar1_r=myVar1 ;
    myVar2_r=myVar2 ;
    ..
    myVar&amp;lt;n&amp;gt;_r=myVar&amp;lt;n&amp;gt; ;
run ;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then this will do it&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mlogic mprint symbolgen ;

/* Create sample data */
data have ;
	infile cards ;
	input myVar1 myVar2 myVar3 ;
cards ;
1 2 3
4 5 6
7 8 9
;

/* Get variables we are interested in from SASHELP.VCOLUMN */
data _null_ ;
	set sashelp.vcolumn 
		(where =(
			libname="WORK" and 
			memname="HAVE" and
			name ? "myVar" )) ;
	/* Create macro variables var&amp;lt;n&amp;gt; that will contain an assignment statement 
       e.g. 
			myVar1_r=myVar1 ;
			myVar2_r=myVar2 ;
			...
			myVar&amp;lt;n&amp;gt;_r=myVar&amp;lt;n&amp;gt; ;
	*/
	call symput("var"!!left(putn(_n_,"8.")),trim(name)!!"_r="!!name!!" ;") ;
	/* Store number of variables we are assigning */
	call symput("varCnt",putn(_n_,"8.")) ;
run ;

/* Macro to assign new variables */
%macro rename(cnt) ;
	/* Create want dataset */
	data want ;
		/* Read have dataset */
		set have ;

		/* loop through the macro variables created in step above */
		%do i=1 %to &amp;amp;cnt ;
			/* debugging %put statement */
			%put var&amp;amp;i : &amp;amp;&amp;amp;var&amp;amp;i ;
			/* resolve the macro variables created in step above 
				e.g. 
			    	myVar1_r=myVar1 ;
					myVar2_r=myVar2 ;
					...
					myVar&amp;lt;n&amp;gt;_r=myVar&amp;lt;n&amp;gt; ;
			*/
			&amp;amp;&amp;amp;var&amp;amp;i ;
		%end ;
	run ;
%mend ;

/* Call the macro, passing number of variables to rename */
%rename(&amp;amp;varCnt) ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Nov 2021 12:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-using-and-based-on-arrays/m-p/778476#M247790</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-11-04T12:10:13Z</dc:date>
    </item>
  </channel>
</rss>

