qfd
qfd

Reputation: 788

list of macro variables to dataset in SAS

I have a macro variable as the below

%let myGrp = ('ctry', 'age')

all i want to do is create a dataset called TestGrp which has the below

GROUPS
ctry
age

I am unable to figure out how to do this. Any help is much appreciated

Thanks!

Upvotes: 1

Views: 581

Answers (2)

Bruno
Bruno

Reputation: 140

This is a working example (sorry for the Spanish names):

%let sListaDeValoresAutorizados= ('ctry', 'age');
%LET LISTA_VALORES = %UNQUOTE(%sysfunc(compress("&sListaDeValoresAutorizados","(')")));

DATA yourdata;
    %DO _t = 1 %TO %sysfunc(countw("&LISTA_VALORES",','));
        GROUPS = strip(scan("&LISTA_VALORES",&_t,","));
        OUTPUT;
    %END;
RUN;

Upvotes: 0

Joe
Joe

Reputation: 63424

SCAN is the function that will help you the most here. Odds are your entire approach could be improved, but given this to start with...

%let myGrp = ('ctry', 'age');
data yourdata;
do _t = 1 to countc("&mygrp",",")+1;
  groups = scan("&myGrp",_t,"'(,) ");
  output;
end;
run;

SCAN takes any number of delimiters, and can be useful here to remove characters you do not want. In the long run, %let mygrp=ctry,age; would be just as well - the quotes etc. aren't helpful.

Upvotes: 3

Related Questions