Help using Base SAS procedures

printing data set

Reply
N/A
Posts: 0

printing data set

I want to print variables which contains "samp" is variable name.

example :
data abc ;
input a1 a3 za ba ;
datalines;
1 2 3 4
4 5 6 7
7 8 9 0
;
proc print data=abc;
var a:;
run;

The above data print all the variables which is starting with A;

in my data set contains some of the variables jan_samp1, feb_samp2 like that.
i want to print the variables which contains "samp" in that variable name.
Contributor
Posts: 61

Re: printing data set

Hello Sairam,

Use Keep Option and print required variables.
N/A
Posts: 0

Re: printing data set

Hi I Have data set like:

Data jan;
input a b a_samp b_samp c_samp d_samp c d e;
datalines;
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 0 1 2
;

i want to print the variables which contains "samp" as variable names
ex: a_samp , b_samp , c_samp , d_samp only from the above data set.

if i use the following code :
proc print data=jan;
var a:;
run;

the output is;

a a_samp
1 3
4 6
;

i want a code like above my out put willl be:

a_samp b_samp c_samp d_samp

3 4 5 6
6 7 8 9
;
N/A
Posts: 0

Re: printing data set

Hi Sairam,

There is no simple solution to this, well nothing a simple as a wildcard like colon. There are solutions. They would involve macro language. There are various ways to find out what variables are in your dataset but the use of dictionary tables here would be my choice. Using SQL to create a macro variable containg the names of the variables you want to keep.

Here is the code I would use:

proc sql noprint;
select lowcase(name) into: keep_vars
from dictionary.columns
where upcase(memname)="JAN" and upcase(libname)="WORK" and substr(reverse(upcase(name)),1,5)="PMAS_";
quit;

then you can use the keep_vars macro variable in your code to select the variables you want. For example:

proc print data=jan;
var &keep_vars;
run;
Respected Advisor
Posts: 3,777

Re: printing data set

Unfortunatly the : only works at the end of a name to to create a SAS Variable List with all names beginning with a character string. To create the list you desire with SAMP at the end you need to use the meta data assocaited with the table. One way that is common uses DICTIONARY.TABLES and the WHERE clause LIKE operator. This example creates a macro variable that can be used later in a VAR statement perhaps.

[pre]
Data jan;
input a b a_samp b_samp c_samp d_samp c d e;
datalines;
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 0 1 2
;;;;
run;
proc sql noprint;
select name into :names separated ' '
from dictionary.columns
where
libname eq 'WORK' and memname eq 'JAN'
and upcase(name) like '%SAMP'
;
quit;
run;
%put NOTE: NAMES=&names;
[/pre]
Ask a Question
Discussion stats
  • 4 replies
  • 126 views
  • 0 likes
  • 3 in conversation