DATA Step, Macro, Functions and more

New variable

Reply
Super Contributor
Posts: 647

New variable

%MACRO OUTPUT(DSN,MARKET,GROUP1,GROUP2,GROUP3);

%DO I=1 %TO 3;
%put GROUP= &&GROUP&I;
%IF %NRQUOTE(&&GROUP&I)= %THEN %DO;
%LET VGROUP&I = ;
%END;
%ELSE %DO;
%LET VGROUP&I = "&&GROUP&I"N;
%END;
%put VGROUP= &&VGROUP&I;

%END;


%mend output;
%OUTPUT(ANTIF,ANTI-FUNGAL
,ABC,DE F,HIJ);


How to create another macro variable name which resolves as follows:
'ABC,'DEF',HIJ'
i.e
name resolves to 'ABC,'DEF',HIJ'
Super Contributor
Super Contributor
Posts: 365

Re: New variable

Hello SASPhile,

You wrote: How to create another macro variable name which resolves as follows:
'ABC,'DEF',HIJ' i.e name resolves to 'ABC,'DEF',HIJ'

%let New='ABC,'DEF',HIJ';

Is this what you need?

Sincerely,
SPR
Super Contributor
Posts: 647

Re: New variable

Yes.
But macro variable 'NEW' are the postional parameters. They may change whenever the user passes a new value.
So it should be dynamic.
Super Contributor
Super Contributor
Posts: 365

Re: New variable

How about this within your macro:

%let new="&GROUP1.","&GROUP2.","&GROUP3.";?
Super Contributor
Posts: 647

Re: New variable

The reason to create %let name is to use it in subsetting the dataset in the next step.
i.e where condition where rolrod in (&name);

The problem with that is:

there could be group1 to group9 values.but sometimes user passes only 6 or 5 or 3 values .
so in the %let name statement if we define from group1 to group9

and only three values are passed
the macro varible will have missing values in the end like this

%let name= 'ABC','DEF',HIJ',,,,,, ;
Regular Contributor
Posts: 241

Re: New variable

%*-- comma separated quoted values --*;
%macro csqv(key1,key2,key3,key4,key5,key6,key7,key8,key9);
   %local i k;
   %do i = 1 %to 9;
      %let k = &&key&i;
      %if &k= %then %goto cont;
      %if &i>1 %then %*;,;
      %*;%sysfunc(quote(&k))
      %cont:
   %end;
%mend csqv;
%put ***%csqv(abc, def, hij)***;
%*-- on log
***"abc","def","hij"***
--*;

Super Contributor
Posts: 647

Re: New variable

%cont is not defined!?
Respected Advisor
Posts: 3,889

Re: New variable

Hi SASPhile

Is it eventually this you're after?

%macro OUTPUT/parmbuff;

%let dsn=%scan(%bquote(&syspbuff),1,%str(%(,%)));
%let MARKET=%scan(%bquote(&syspbuff),2,%str(,%)));

%put dsn = &dsn;
%put market= &market;

%let i=3;
%let name=;
%do %while(%scan(%bquote(&syspbuff),&i,%str(,%))) ne );
%let group&i =%scan(%bquote(&syspbuff),&i,%str(,%)));
%let vgroup&i="&&group&i"N;
%if &name eq %then %let name="&&group&i";
%else %let name=&name.,"&&group&i";

%put group= &&GROUP&I;
%put VGROUP= &&VGROUP&I;

%let i=%eval(&i+1);
%end;

%put name= &name;

%mend OUTPUT;

%OUTPUT(ANTIF,ANTI-FUNGAL,ABC,DE F,HIJ)

%OUTPUT(ANTIF,ANTI-FUNGAL,ABC,DE F,HIJ, xxxx, yy yy)


HTH
Patrick Message was edited by: Patrick
Super Contributor
Posts: 647

Re: New variable

if one of the parameters is like this ABC/DEF?
Respected Advisor
Posts: 3,889

Re: New variable

Hi SASPhile

"if one of the parameters is like this ABC/DEF?"

You're the one who knows best what strings could be passed - and probabely you need to play around a bit more with quoting and unquoting.

For the case you've given using %qscan() instead of %scan() in the %do..%while loop does the trick:
%do %while(%qscan(&syspbuff,&i,%str(,%))) ne );

HTH
Patrick
Super Contributor
Posts: 647

Re: New variable

Hi,
How to insert a record?
when passing the parameters say ABC is not present in the dataset, we need to insert a record for ABC in the dataset.
Super User
Posts: 9,676

Re: New variable

[pre]
data class;
set sashelp.class;
run;
proc sql;
insert into class
set name='ABC',age=10;
quit;
[/pre]

Ksharp
Super User
Posts: 9,676

Re: New variable

How about:
[pre]

%MACRO OUTPUT(DSN,MARKET,GROUP1,GROUP2,GROUP3);
%let name=;
%let group1=%sysfunc(compress(&group1));
%let group2=%sysfunc(compress(&group2));
%let group3=%sysfunc(compress(&group3));

%DO I=1 %TO 3;
%let name=&name %str(%')&&group&i%str(%');
%END;
%let name=%sysfunc(translate(&name,%str(,),%str( )));
%put name= &name;

%mend output;
%OUTPUT(ANTIF,ANTI-FUNGAL,ABC,DE F,HIJ)

[/pre]

Ksharp

Message was edited by: Ksharp

Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 12 replies
  • 225 views
  • 0 likes
  • 5 in conversation