DATA Step, Macro, Functions and more

Need Help writing macro array and transpose statements

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Need Help writing macro array and transpose statements

*Working code...;

 

data test;
set ssh.sshbindicatorstable28jul2016;
run;
 
proc contents data=test; run;
 
proc sort data =test;
by hsnum year sex age arhsres: wt: disadvqt denom schpatt sch supptype year: agegrp region
ariaplusc lhd10 phn_2015_code pcode pcoderes indigene;
run;
 
Data m_mental5 (keep= hsnum year sex age arhsres: wt: disadvqt denom schpatt sch supptype year: agegrp region
                             ariaplusc lhd10 phn_2015_code pcode pcoderes indigene m_mental5 m_mental5_txt);
set test;
m_mental5_txt_1=m_mental5_1_txt;
m_mental5_txt_2=m_mental5_2_txt;
m_mental5_txt_3=m_mental5_3_txt;
m_mental5_txt_4=m_mental5_4_txt;
m_mental5_txt_5=m_mental5_5_txt;
m_mental5_txt_6=m_mental5_6_txt;
m_mental5_txt_7=m_mental5_7_txt;
m_mental5_txt_8=m_mental5_8_txt;
array a (8) m_mental5_1-m_mental5_8;
array b (8) m_mental5_txt_1-m_mental5_txt_8;
do i=1 to 8;
   m_mental5= a(i);
   m_mental5_txt=b(i);
if not missing (m_mental5_txt) then output;
end;
run;
 
*Macro code generating error;
%macro multiresponse (ds=);
 
Data &ds (keep= hsnum year sex age arhsres: wt: disadvqt denom schpatt sch supptype year: agegrp region
                             ariaplusc lhd10 phn_2015_code pcode pcoderes indigene &ds &ds._txt);
set test;
 
%do i=1 %to 8;
  &&ds_txt_i=&&ds_i_txt;
  %end;
run;
array a (*) &ds_1-&ds_8;
array b (*) &ds._txt_1-&ds._txt_8;
do i=1 to dim(&ds._txt_8)
    &ds= a(i);
     &ds_txt=b(i);
if not missing (&ds._txt) then output;
run;
end;
%mend;
 
%multiresponse (ds=m_mental5);
 
 *Also tried using transpose but again generating error;
 
%macro msh(ds=, vars=);
proc transpose data=test out=&ds (drop=_name_ rename=(_label_=Category Col1=Value));
by hsnum year sex age arhsres: wt: disadvqt denom schpatt sch supptype year: agegrp region
ariaplusc lhd10 phn_2015_code pcode pcoderes indigene;
VAR &vars;
run;
%mend;
 
%msh(ds=m_mental5, vars=&ds1 &ds2 &ds3 &ds4);

Accepted Solutions
Solution
‎08-08-2016 08:47 AM
Respected Advisor
Posts: 3,894

Re: Need Help writing macro array and transpose statements

[ Edited ]

Everything @LinusH and @RW9 said.

 

...but what certainly will throw an error is a "run" statement in the middle of a data step

*Macro code generating error;
%macro multiresponse (ds=);
 
Data &ds.....
set test;
.....
run;
array a (*) &ds_1-&ds_8;
.....
run;
end;
%mend;

 ...oh... and an "end" which is not an "%end" after a "run" statement.

 

What you probably should do: Make your data step work as a sample outside of a macro and only "macrotize" it once it works. 

View solution in original post


All Replies
Super User
Posts: 5,257

Re: Need Help writing macro array and transpose statements

So, what's the error?

Data never sleeps
Super User
Super User
Posts: 7,407

Re: Need Help writing macro array and transpose statements

Hi,

 

Lets take a step back.  Post some test data:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

With what you have, and show an example of what you want the output to look like.  Most of that code posted there looks to be redundant, but until I see input/output its hard to say.

Solution
‎08-08-2016 08:47 AM
Respected Advisor
Posts: 3,894

Re: Need Help writing macro array and transpose statements

[ Edited ]

Everything @LinusH and @RW9 said.

 

...but what certainly will throw an error is a "run" statement in the middle of a data step

*Macro code generating error;
%macro multiresponse (ds=);
 
Data &ds.....
set test;
.....
run;
array a (*) &ds_1-&ds_8;
.....
run;
end;
%mend;

 ...oh... and an "end" which is not an "%end" after a "run" statement.

 

What you probably should do: Make your data step work as a sample outside of a macro and only "macrotize" it once it works. 

New Contributor
Posts: 2

Re: Need Help writing macro array and transpose statements

Thank you.

Super User
Posts: 5,085

Re: Need Help writing macro array and transpose statements

In addition, note that your definition of the elements in the array A is incorrect.  The array B is correct, but A should use similar syntax:

 

&ds._1 - &ds._2

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 319 views
  • 1 like
  • 5 in conversation