<?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: Return variable from a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52803#M11181</link>
    <description>That explains it! Thank you so much.&lt;BR /&gt;
I have an issue when the Tranwrd fundtion is passed dates. Though the input to the Tranwrd is 7/14/09 , the output is 71409. The same thing happens with brackets.If the input text has some brackets, the output does not have them. &lt;BR /&gt;
&lt;BR /&gt;
Any suggestions?</description>
    <pubDate>Tue, 20 Jul 2010 16:46:33 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-07-20T16:46:33Z</dc:date>
    <item>
      <title>Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52795#M11173</link>
      <description>This is my first SAS assgnmt so my question is really basic.&lt;BR /&gt;
I have  a table which can has 2 fields Fld, Attr. Now I am doing a transpose on this table, so the no. of 'Fld' and 'Attr' really depends on data. eg. Fld1, Fl2...Attr1 Attr2 etc. &lt;BR /&gt;
&lt;BR /&gt;
Now I need macro which will count how many of FLd/Attr variables are there and then pass this to the following piece of code as iterations. So instead of having the loop as 1 to 5, I want to pass the iterations to it. &lt;BR /&gt;
Basically, it does not recognise fld/attr variable as the number is not suffixed.&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 select distinct attr, count(*)  from  mydata.e_joinf; &lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data MYDATA.e_final;&lt;BR /&gt;
set mydata.e_join;&lt;BR /&gt;
array fld(5);&lt;BR /&gt;
array attr(5); &lt;BR /&gt;
do j=1 to 5;&lt;BR /&gt;
fld_txt = fld(j);&lt;BR /&gt;
fld_name = attr(j);&lt;BR /&gt;
msg_txt =tranwrd(msg_txt,trim(fld_name),trim(fld_txt));&lt;BR /&gt;
msg_txt = compress(msg_txt,'()');&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Can someone please help me out? &lt;BR /&gt;
&lt;BR /&gt;
Thanks!

Message was edited by: sasnewbee</description>
      <pubDate>Mon, 19 Jul 2010 14:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52795#M11173</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-19T14:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52796#M11174</link>
      <description>You don't need a macro just to tell you how many variables you have, presuming that the named variables have a unique variable prefix, such as you have mentioned.&lt;BR /&gt;
&lt;BR /&gt;
An ARRAY statement can declare a SAS variable prefix using a trailing colon character -- then you can use the DIM(&lt;ARRAYNAME&gt;) function in a DO statement to iterate through your SAS  variable list without listing the variables each by name.&lt;BR /&gt;
&lt;BR /&gt;
Here's a simple example:&lt;BR /&gt;
&lt;BR /&gt;
DATA _null_;&lt;BR /&gt;
retain v1-v5 1;&lt;BR /&gt;
array av_vars (*) v: ;&lt;BR /&gt;
do i=1 to dim(av_vars);&lt;BR /&gt;
  putlog av_vars(i)= ;&lt;BR /&gt;
end;&lt;BR /&gt;
putlog _all_;&lt;BR /&gt;
stop;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
SAS SGF/SUGI conference paper on the topic:&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf" target="_blank"&gt;http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf&lt;/A&gt;

Message was edited by: sbb&lt;/ARRAYNAME&gt;</description>
      <pubDate>Mon, 19 Jul 2010 15:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52796#M11174</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-19T15:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52797#M11175</link>
      <description>I did try using the colon and then the variable name but still the log says variallble attr not found.&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 select count(distinct attr) into: vars  from  mydata.e_csa.&lt;BR /&gt;
&lt;BR /&gt;
And secondly when we say retain v1-v5 , are we not hardcoding the maximum value to 5?&lt;BR /&gt;
I am going thru the link too , to find if I get any clues.

Message was edited by: sasnewbee</description>
      <pubDate>Mon, 19 Jul 2010 15:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52797#M11175</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-19T15:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52798#M11176</link>
      <description>Please review and ingest the sample code provided in the prior reply - it works.....  You want to leverage SAS programming techniques as much as possible.  One way is to avoid using macro variables and the PROC SQL altogether, as suggested.&lt;BR /&gt;
&lt;BR /&gt;
And, if you tried some code and didn't get expected results, suggest you share the SAS log with all output revealed -- also you may find the problem source by activating the most diagnostic output, with this stmt:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MLOGIC MPRINT;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 19 Jul 2010 16:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52798#M11176</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-19T16:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52799#M11177</link>
      <description>Yes this code works. What I was thinking earlier was i still have to find the count using SQL and then use the dim function. But this was easy. I did get the results but still need a few changes. &lt;BR /&gt;
Can you please let me know if I need the retain statement? I read that the retain stmt is used to retain previous values. So I changed that stmt to retain attr1-attr5. &lt;BR /&gt;
I got an error : retain attr1-attr5 1;&lt;BR /&gt;
ERROR: 1 and attr1 are incompatible for retain.&lt;BR /&gt;
ERROR: 1 and attr2 are incompatible for retain.&lt;BR /&gt;
&lt;BR /&gt;
I also get 3 additional columns: Fld_name,fld_txt and i. IS there any way to drop these? &lt;BR /&gt;
&lt;BR /&gt;
data MYDATA.e_finalf;&lt;BR /&gt;
set mydata.e_joinf;&lt;BR /&gt;
*retain v1-v5 1;&lt;BR /&gt;
array fld_nm_var (*) attr: ;&lt;BR /&gt;
array fld_txt_var(*) fld:;&lt;BR /&gt;
do i=1 to dim(fld_nm_var);&lt;BR /&gt;
fld_txt = fld_txt_var(i);&lt;BR /&gt;
fld_name = fld_nm_var(i);&lt;BR /&gt;
msg_txt =tranwrd(msg_txt,trim(fld_name),trim(fld_txt));&lt;BR /&gt;
msg_txt = compress(msg_txt,'()');&lt;BR /&gt;
PUT '****';&lt;BR /&gt;
end;&lt;BR /&gt;
run;</description>
      <pubDate>Mon, 19 Jul 2010 16:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52799#M11177</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-19T16:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52800#M11178</link>
      <description>Why do you think you need a RETAIN statement at all?  There you go again with that hardcoded "5" suffix.....&lt;BR /&gt;
&lt;BR /&gt;
Also, you can use either DROP or KEEP statement, as preferred.  Note that when both are coded, DROP overrides KEEP when they both list the same variable.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 19 Jul 2010 18:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52800#M11178</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-19T18:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52801#M11179</link>
      <description>Thanks Scott. I removed the retain statement and also added the Drop. &lt;BR /&gt;
But I was wondering why SAS appends new columns to an existing dataset? &lt;BR /&gt;
I was also under the assumption that Drop stmts etc will work only in a Proc SQL.</description>
      <pubDate>Mon, 19 Jul 2010 19:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52801#M11179</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-19T19:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52802#M11180</link>
      <description>If you have a DATA step and you derive a new SAS variable, that variable is added to the SAS member(s) on the DATA statement, unless you use a DROP, KEEP statement or a DROP= / KEEP=  dataset_option.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Step-by-Step Programming with Base SAS(R) Software&lt;BR /&gt;
--Introduction to DATA Step Processing&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001302699.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001302699.htm&lt;/A&gt;</description>
      <pubDate>Mon, 19 Jul 2010 19:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52802#M11180</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-19T19:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52803#M11181</link>
      <description>That explains it! Thank you so much.&lt;BR /&gt;
I have an issue when the Tranwrd fundtion is passed dates. Though the input to the Tranwrd is 7/14/09 , the output is 71409. The same thing happens with brackets.If the input text has some brackets, the output does not have them. &lt;BR /&gt;
&lt;BR /&gt;
Any suggestions?</description>
      <pubDate>Tue, 20 Jul 2010 16:46:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52803#M11181</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-20T16:46:33Z</dc:date>
    </item>
    <item>
      <title>Re: Return variable from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52804#M11182</link>
      <description>Yes - open a new forum thread first, and post a more appropriate SUBJECT.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 20 Jul 2010 17:02:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Return-variable-from-a-macro/m-p/52804#M11182</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-20T17:02:47Z</dc:date>
    </item>
  </channel>
</rss>

