<?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: Basic looping question using macro variable and observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279808#M56431</link>
    <description>&lt;P&gt;Macro statements are not part of a DATA step.&amp;nbsp; %DO, and %IMPORT cannot be executed within a DATA step.&amp;nbsp; The most they can do is construct SAS language statements that become part of the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix the problem, here is one approach that changes the looping.&amp;nbsp; Within the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;%if &amp;amp;ilkup_cnt. ge 1 %then %do i=1 %to &amp;amp;ilkup_cnt.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;data _null_;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;set outdir.imp_lkup_files (firstobs=&amp;amp;i obs=&amp;amp;i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_dsn',dsn);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_sheet',xlsheet);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_file',filenm);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; run;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%import(out_dsn=&amp;amp;imp_dsn.,file=&amp;amp;imp_file.,shee&lt;WBR /&gt;t=&amp;amp;imp_sheet.)&lt;BR /&gt;&amp;nbsp;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jun 2016 19:27:45 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-06-23T19:27:45Z</dc:date>
    <item>
      <title>Basic looping question using macro variable and observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279801#M56428</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a basic looping question inside a dataset to call the import macro based on the total number of import file list given in the source data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data outdir.imp_lkup_files;&lt;BR /&gt;&amp;nbsp;length dsn $40 xlsheet $20 filenm $150;&lt;BR /&gt;&amp;nbsp;infile datalines DSD Delimiter='|';&lt;BR /&gt;&amp;nbsp;input dsn $ xlsheet $ filenm $;&lt;BR /&gt;datalines;&lt;BR /&gt;outdir.Feb|planf|K:\user\plan_f_20160204&lt;BR /&gt;outdir.Mar|planm|K:\user\plan_m_20160322&lt;BR /&gt;outdir.Apr|plana|K:\user\plan_a_20160407&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This source dataset contains 3 observations which i have stored in a macro variable ilkup_cnt = 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, i have a macro to import the files into a dataset dynamically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro import(out_dsn=,file=,sheet=);&lt;BR /&gt;PROC IMPORT OUT= &amp;amp;out_dsn. DATAFILE= &amp;amp;file.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DBMS=xlsx REPLACE;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SHEET=&amp;amp;sheet.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GETNAMES=YES;&lt;BR /&gt;RUN;&lt;BR /&gt;%mend import;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am calling this macro in another main macro called file_imp_exp, like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro file_imp_exp(action=);&lt;BR /&gt;%if %upcase(&amp;amp;action)=IMPORT %then %do;&lt;BR /&gt;&amp;nbsp;%if &amp;amp;ilkup_cnt. ge 1 %then %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;data _null_;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;set outdir.imp_lkup_files;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;%do i = 1 %to &amp;amp;ilkup_cnt.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_dsn',dsn);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_sheet',xlsheet);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_file',filenm);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%import(out_dsn=&amp;amp;imp_dsn.,file=&amp;amp;imp_file.,sheet=&amp;amp;imp_sheet.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;run;&lt;BR /&gt;&amp;nbsp;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if %upcase(&amp;amp;action)=EXPORT %then %do;&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;%end;&lt;BR /&gt;%mend file_imp_exp;&lt;/P&gt;&lt;P&gt;%file_imp_exp(action=import);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can see only the 3rd file is imported into a dataset but not the first and second. Can you please suggest how to use the looping between the observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 19:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279801#M56428</guid>
      <dc:creator>ArunprasadT</dc:creator>
      <dc:date>2016-06-23T19:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Basic looping question using macro variable and observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279808#M56431</link>
      <description>&lt;P&gt;Macro statements are not part of a DATA step.&amp;nbsp; %DO, and %IMPORT cannot be executed within a DATA step.&amp;nbsp; The most they can do is construct SAS language statements that become part of the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix the problem, here is one approach that changes the looping.&amp;nbsp; Within the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;%if &amp;amp;ilkup_cnt. ge 1 %then %do i=1 %to &amp;amp;ilkup_cnt.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;data _null_;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;set outdir.imp_lkup_files (firstobs=&amp;amp;i obs=&amp;amp;i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_dsn',dsn);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_sheet',xlsheet);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call symput('imp_file',filenm);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; run;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%import(out_dsn=&amp;amp;imp_dsn.,file=&amp;amp;imp_file.,shee&lt;WBR /&gt;t=&amp;amp;imp_sheet.)&lt;BR /&gt;&amp;nbsp;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 19:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279808#M56431</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-23T19:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: Basic looping question using macro variable and observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279831#M56437</link>
      <description>&lt;P&gt;You may want to look into call execute, especially if you have your parameters in a data set.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 19:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279831#M56437</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-23T19:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Basic looping question using macro variable and observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279924#M56487</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;has stated, add one procedure to your datastep and avoid all that messy code:&lt;/P&gt;
&lt;PRE&gt;data outdir.imp_lkup_files;
  length dsn $40 xlsheet $20 filenm $150;
  infile datalines DSD Delimiter='|';
  input dsn $ xlsheet $ filenm $;
/* Here */
  call execute(cat('proc import datafile=',strip(filenm),' out=',strip(dsn),' dbms=xlsx replace; sheet="',strip(xlsheet),'"; getnames=yes; run;'));
datalines;
outdir.Feb|planf|K:\user\plan_f_20160204
outdir.Mar|planm|K:\user\plan_m_20160322
outdir.Apr|plana|K:\user\plan_a_20160407
;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jun 2016 08:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-looping-question-using-macro-variable-and-observation/m-p/279924#M56487</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-06-24T08:22:31Z</dc:date>
    </item>
  </channel>
</rss>

