<?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: empty macro variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/empty-macro-variable/m-p/571129#M12095</link>
    <description>&lt;P&gt;You need something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;By.) %then %do;
  by &amp;amp;By,;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in your proc transpose.&lt;/P&gt;</description>
    <pubDate>Thu, 04 Jul 2019 05:05:26 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-07-04T05:05:26Z</dc:date>
    <item>
      <title>empty macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/empty-macro-variable/m-p/571123#M12092</link>
      <description>&lt;P&gt;I have writing a transpose macro and I am trying to get the macro variable by statement if there is no by variables then run a transpose without the by. if there is a by statement then run the transpose with the by statement. In the code below I have it with a by statement and it works. But if the data doesn't need a by statement then it will produce errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO transpose (DATAIN=,DATAOUT=,BY=,ID=,IDLABEL=,&lt;BR /&gt;VAR= ,Prefix=);&lt;/P&gt;&lt;P&gt;%global dset nvars nobs;&lt;BR /&gt;%let dset=&amp;amp;datain;&lt;BR /&gt;%let dsid = %sysfunc(open(&amp;amp;dset));&lt;BR /&gt;%if &amp;amp;dsid %then %do;&lt;BR /&gt;&lt;BR /&gt;%let nobs =%sysfunc(attrn(&amp;amp;dsid,NOBS)); /* counts number of obs */&lt;BR /&gt;%let nvars=%sysfunc(attrn(&amp;amp;dsid,NVARS));/* counts number of variables in dataset */&lt;BR /&gt;%let rc = %sysfunc(close(&amp;amp;dsid));/* close the dataset */&lt;BR /&gt;%put &amp;amp;dset has &amp;amp;nvars variable(s) and &amp;amp;nobs observation(s).;/*prints out the values of &amp;amp;dset and &amp;amp;nvars to log */&lt;BR /&gt;%end;&lt;BR /&gt;%else&lt;BR /&gt;%put Open for data set &amp;amp;dset failed - %sysfunc(sysmsg());/*if the data set in the datain is not a dataset in work lib then it will print error message to log*/&lt;/P&gt;&lt;P&gt;proc sort data=&amp;amp;datain noduprecs dupout=duplicates;by &amp;amp;by;run;/*checks the &amp;amp;datain for duplicate records*/&lt;/P&gt;&lt;P&gt;proc sql; select count(*) into: ia from duplicates; quit;/*store counts into ia*/&lt;/P&gt;&lt;P&gt;/* if the datain has ID values the occur twice in the by group statement then it will create a data set using &amp;amp;dataout with error message */&lt;BR /&gt;%if &amp;amp;ia ^= 0 %then %do;&lt;/P&gt;&lt;P&gt;data &amp;amp;dataout;&lt;BR /&gt;&lt;BR /&gt;col="The ID value xxxxxxxx occurs twice in the same BY group";&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;/* if no repeat values then run the rest of the macro code*/&lt;BR /&gt;%if &amp;amp;ia = 0 %then %do;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* sort macro*/&lt;BR /&gt;%let sortstr=&amp;amp;by;&lt;BR /&gt;%macro issorted(dsname);&lt;BR /&gt;%let dsid=%sysfunc(open(&amp;amp;dsname));&lt;BR /&gt;%if (&amp;amp;dsid=0) %then&lt;BR /&gt;%put MSG=%sysfunc(sysmsg());&lt;BR /&gt;%else %do;&lt;BR /&gt;%let seq=%sysfunc(attrc(&amp;amp;dsid,sortedby));/*sortedby indicates how the dataset is sorted but does not sort the data*/&lt;/P&gt;&lt;P&gt;%let rc=%sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;%if %sysfunc(strip(&amp;amp;sortstr)) ne %sysfunc(strip(&amp;amp;seq)) %then %do;&lt;BR /&gt;proc sort data=&amp;amp;dsname;&lt;BR /&gt;by &amp;amp;sortstr;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%else %put A sort was not required;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%issorted(&amp;amp;datain);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if %sysfunc(exist(&amp;amp;datain)) %then %do;&lt;/P&gt;&lt;P&gt;PROC TRANSPOSE DATA = &amp;amp;datain&lt;BR /&gt;OUT = &amp;amp;dataout(DROP = _name_)&lt;BR /&gt;PREFIX = &amp;amp;prefix;&lt;BR /&gt;&lt;BR /&gt;BY &amp;amp;by ;&lt;BR /&gt;VAR &amp;amp;var;&lt;BR /&gt;ID &amp;amp;id;&lt;BR /&gt;RUN;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if %sysfunc(exist(&amp;amp;datain))= 0 %then %do ;&lt;BR /&gt;%put the data set &amp;amp;datain does not exist.;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;options mprint mlogic;&lt;BR /&gt;%end;&lt;BR /&gt;%MEND;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2019 03:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/empty-macro-variable/m-p/571123#M12092</guid>
      <dc:creator>chrissowden</dc:creator>
      <dc:date>2019-07-04T03:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: empty macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/empty-macro-variable/m-p/571129#M12095</link>
      <description>&lt;P&gt;You need something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;By.) %then %do;
  by &amp;amp;By,;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in your proc transpose.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2019 05:05:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/empty-macro-variable/m-p/571129#M12095</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-07-04T05:05:26Z</dc:date>
    </item>
  </channel>
</rss>

