DATA Step, Macro, Functions and more

To create data set based on multiple macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 76
Accepted Solution

To create data set based on multiple macro variables

Dear all,

 

I created multiple macro vairable stores differnt number of patients cnt using proc sql statements.

 

I would like to put these macro vairables together using the following data step, however, there is error message.  I am wondering whether you can help me with how to put those variable together .  Thank you very much !

 

data cnt;

      input cnt1   mark $ 30;

      cards;

&peme                               peme

&beva                               beva

&peme_beva                          peme_beva

&IOIO                                     IOIO

&IOBF                                     IOBF

&IOAF                                     IOAF

&cntline2                           cntline2

&cntline2_chemo                     cntline2_chemo

&cntline2_cohort                    cntline2_cohort

&cntline2_chemo_cohort        cntline2_chemo_cohort

&Refra                                     Refra

&refra_12_y                         refra_12_y

&refra_12_n                         refra_12_n

&refra_9_y                          refra_9_y

&refra_9_n                          refra_9_n

&refra_12_y_b                       refra_12_y_b

&refra_12_n_b                 refra_12_n_b

&refra_9_y_b                        refra_9_y_b

&refra_9_n_b                        refra_9_n_b

;

run;


Accepted Solutions
Solution
‎09-13-2016 09:12 AM
Super User
Posts: 9,671

Re: To create data set based on multiple macro variables

That is bad coding, Try symget() to retrieve these macro variables.




%let peme=x;
%let beva=y;
data cnt;
      input  mark $;
cnt1=symget(mark);
cards;           
peme
beva
;
run;

View solution in original post


All Replies
Solution
‎09-13-2016 09:12 AM
Super User
Posts: 9,671

Re: To create data set based on multiple macro variables

That is bad coding, Try symget() to retrieve these macro variables.




%let peme=x;
%let beva=y;
data cnt;
      input  mark $;
cnt1=symget(mark);
cards;           
peme
beva
;
run;

Super User
Posts: 17,775

Re: To create data set based on multiple macro variables

As noted, you'd be better off storing the values as you calculated them, rather than after the fact. 

Super User
Posts: 6,928

Re: To create data set based on multiple macro variables

Within a cards; block, macro triggers won't work.

Use the symget function to retrieve macro values instead, as @Ksharp already suggested.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,392

Re: To create data set based on multiple macro variables

Could we take a step back here.  What is it your actually trying to do - test data/required output.  Why for instance are you counting numbers of X, and then putting that into a macro variable, then reading that data into a dataset?  It seems to be a very log way around doing a simple insert:

 

proc sql;
  insert into RESULTS
  set TEST="ABC",
        RESULT=(select count(distinct SUBJECT) from ABC);
quit;

However, even that seems a bit convoluted.  Why not just use the summary procedures and such like?  If its different datasets, and you need a count by dataset then here is simple example of how to combine the datasets (in your case you would just need to keep subject variable) then use the name of the dataset to create one dataset output with counts per dataset:

data tmp1 tmp2;
  set sashelp.class;
  if sex="M" then output tmp1;
  else output tmp2;
run;

data have;
  length name $20;
  set tmp: indsname=n;
  name=n;
run;

proc sql;
  create table WANT as
  select  NAME,
          AGE,
          count(*) as RESULT
  from    HAVE
  group by NAME,AGE;
quit; 

As with anything there is never a need to resort to macro code.

 

Frequent Contributor
Frequent Contributor
Posts: 76

Re: To create data set based on multiple macro variables

Dear all,

 

Thank you very much for your valueable knowledge !

 

That really helps !

 

Ivy 

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 306 views
  • 0 likes
  • 5 in conversation