Help using Base SAS procedures

How do we create both distinct variables and number of variable counts in sql macro

Accepted Solution Solved
Reply
Contributor gsk
Contributor
Posts: 25
Accepted Solution

How do we create both distinct variables and number of variable counts in sql macro

I want to create macro variables of lists of numeric, character, and date variable (separately) and their respective number of variables. Below is my code so far but I don't know how to add codes to create macros to store the number of each variable type. 

 

proc sql noprint;
    select name into :numlist separated by ' ',
 / * HERE.. I don't know what to write. something like len(&numlist) or count(name) into :numvalcount is what I want to do. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='num' AND FORMAT^='DATE9.'
            order by name;
   
   select name into :datelist separated by ' ', 

 / * HERE.. I don't know what to write. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='num' AND FORMAT='DATE9.'
            order by name;

   select name into :charlist separated by ' ', 

 / * HERE.. I don't know what to write. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='char'
            order by name;
quit;

 

 

So in the end I want to have:

%put &numlist;

%put &charlist;

%put &datelist;

%put &numvalcount;

%put &charvalcount;

%put &datevalcount;


Accepted Solutions
Solution
‎04-15-2018 05:40 PM
Esteemed Advisor
Posts: 5,543

Re: How do we create both distinct variables and number of variable counts in sql macro

[ Edited ]

This syntax works:

 

proc sql noprint;
select name, count(name) into :numlist separated by ' ', :numNumber 
from dictionary.columns
where libname='SASHELP' and memname='CLASS' and
type='num' AND FORMAT ne 'DATE9.'
order by name;

%put &numlist;
%put &numNumber;

 

PG

View solution in original post


All Replies
PROC Star
Posts: 514

Re: How do we create both distinct variables and number of variable counts in sql macro

[ Edited ]

simplest I can think of is 

proc sql noprint;
select name into
    :numlist SEPARATED by ' '
  from dictionary.columns
            where libname='SASHELP' and memname='CARS' and
                        type='num' AND FORMAT^='DATE9.'
order by name;

%let numvalcount= %sysfunc(countw(&numlist));


%put value of numeric columns is &numlist ;
%put value of count columns is &numvalcount;
Solution
‎04-15-2018 05:40 PM
Esteemed Advisor
Posts: 5,543

Re: How do we create both distinct variables and number of variable counts in sql macro

[ Edited ]

This syntax works:

 

proc sql noprint;
select name, count(name) into :numlist separated by ' ', :numNumber 
from dictionary.columns
where libname='SASHELP' and memname='CLASS' and
type='num' AND FORMAT ne 'DATE9.'
order by name;

%put &numlist;
%put &numNumber;

 

PG
Trusted Advisor
Posts: 1,837

Re: How do we create both distinct variables and number of variable counts in sql macro

[ Edited ]

You may even try by:

 

proc freq data=sashelp.vcolumn
              (where=(libname='MET' and memname='DEMA_669'));
        table type * format;
run;
Super User
Posts: 6,785

Re: How do we create both distinct variables and number of variable counts in sql macro

SQL does this for you.  You just have to capture the information before it disappears:

 

proc sql noprint;
    select name into :numlist separated by ' ',
 / * HERE.. I don't know what to write. something like len(&numlist) or count(name) into :numvalcount is what I want to do. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='num' AND FORMAT^='DATE9.'
            order by name;

     %let numvalcount = &sqlobs;
   
   select name into :datelist separated by ' ', 

 / * HERE.. I don't know what to write. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='num' AND FORMAT='DATE9.'
            order by name;

      %let datevalcount = &sqlobs;

   select name into :charlist separated by ' ', 

 / * HERE.. I don't know what to write. */
            from dictionary.columns
            where libname='MET' and memname='DEMA_669' and
                        type='char'
            order by name;

      %let charvalcount = &sqlobs;
quit;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 209 views
  • 6 likes
  • 5 in conversation