DATA Step, Macro, Functions and more

loop column names using macro

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

loop column names using macro

i have column names like  below, if data missing in that column insert null else insert App_id

 

proc contents data=patch.Windows_patch out=patch.contents noprint; run;

proc sql ;
   select name into :varlist separated by ' '
  from patch.contents where name like '%CHG%';
%let n=&obs;
quit;

 

data work;

set have;

do i = 1 to &n ;
    if &Patch {i} =' ' then
        &patch{i}=' '
    else
        &patch{i}=App_ID;
end;
run;

 

 

 

 


Accepted Solutions
Solution
‎04-17-2018 09:04 AM
Super User
Posts: 10,578

Re: loop column names using macro

And make the creation of varlist simpler:

proc sql noprint ;
select name into :varlist separated by ' '
from dictionary.columns
where
  libname = "PATCH" and
  memname = "WINDOWS_PATCH" and
  name like '%CHG%';
quit;

No proc contents needed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 24,012

Re: loop column names using macro

Use a data step and an array instead. SQL doesn't lend itself well to variable lists and multiple variables well and then you can avoid using a macro entirely. 

 


@radha009 wrote:

i have column names like  below, if data missing in that column insert null else insert App_id

 

proc contents data=patch.Windows_patch out=patch.contents noprint; run;

proc sql ;
   select name into :varlist separated by ' '
  from patch.contents where name like '%CHG%';
%let n=&obs;
quit;

 

data work;

set have;

do i = 1 to &n ;
    if &Patch {i} =' ' then
        &patch{i}=' '
    else
        &patch{i}=App_ID;
end;
run;

 

 

 

 


 

 

Super User
Posts: 24,012

Re: loop column names using macro

Sorry, must have misread your question. You have everything you need to do this and you don't need macros. 

If the variables were listed side by side you could also use:

 

array PATCH(*) FirstVarName -- LastVarName;

 

 

data work;

set have;
array Patch (*) &varList.;

do i = 1 to dim(Patch) ;
    if not missing(patch(i)) then patch(i) = APP_ID;
end;


run;

 
Solution
‎04-17-2018 09:04 AM
Super User
Posts: 10,578

Re: loop column names using macro

And make the creation of varlist simpler:

proc sql noprint ;
select name into :varlist separated by ' '
from dictionary.columns
where
  libname = "PATCH" and
  memname = "WINDOWS_PATCH" and
  name like '%CHG%';
quit;

No proc contents needed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 122 views
  • 1 like
  • 3 in conversation