BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
4 REPLIES 4
venkatesh
Calcite | Level 5
Hello Sairam,

Use Keep Option and print required variables.
deleted_user
Not applicable
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
;
deleted_user
Not applicable
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;
data_null__
Jade | Level 19
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]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 810 views
  • 0 likes
  • 3 in conversation