Help using Base SAS procedures

How to print select data from dataset

Reply
Occasional Contributor
Posts: 5

How to print select data from dataset

Hi, New to SAS. I need to write select records based on a username from a dataset to a text file.

What would be the way?

I have the sorted dataset - sorted by username. Need to write records corresponding to the username to a file.

Sorted by username

Code:

Proc sort data=all_files;
by username;
run;

data smith;
set all_files;
file '/directory/smith.txt' PRINT;
where username='smith''

Now this is where I get lost. Was told to possibly use the PUT command but can't make it work correctly.

Any help is greatly appreciated.

Thanks.
Super Contributor
Posts: 292

Re: How to print select data from dataset

If you already have a SAS dataset and you now want a subset dataset you can do this;

data smith;
set all_files (where=( upcase(username)='SMITH''));
run;

If you just want to print the subset from the master file:

proc print data=all_files (where=(upcase(username)='SMITH'));
run;

There is no need to sort for the steps shown above
Occasional Contributor
Posts: 5

Re: How to print select data from dataset

I have the dataset but need to create a text file on a Linux server with just that one user's information in it.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to print select data from dataset

You've got some serious PUT statement syntax issues, related to double- and single-quote marks. Suggest you paste your SAS log in a forum reply, if you would like some assistance / guidance debugging your SAS program?

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 5

Re: How to print select data from dataset

28
29 data smith;
30 set sasuser.all_files;
31 file '/directory/smith.txt' PRINT;
32 where owner='smith';
33 put owner '/directory/smith.txt';
34 run;

NOTE: The file '/directory/smith.txt' is:
Filename=/directory/smith.txt,
Owner Name=xxxxx,Group Name=xxx,
Access Permission=rw-------,
Last Modified=Fri Jul 16 13:31:19 2010

NOTE: 1 record was written to the file '/directory/smith.txt'.
The minimum record length was 31.
The maximum record length was 31.
NOTE: There were 1 observations read from the data set SASUSER.ALL_FILES.
WHERE owner='smith';
NOTE: The data set WORK.smith has 1 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.09 seconds
cpu time 0.01 seconds

output in file is: smith /directory/smith.txt
Super Contributor
Super Contributor
Posts: 3,174

Re: How to print select data from dataset

Can you explain what the problem is?

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 5

Re: How to print select data from dataset

The problem is with the output. It is creating the file - smith.txt but the data in the file is wrong. It is just putting in the words I used in the 'put' command - ie smith /directory/smith.txt instead of something that should look like this:

165 bytes smith 25JUL2009 /directory/testing/filename.txt - this is the information in the dataset that has been created. There are many listed by various users so I need to create a text file containing all of the files that belong to a particular user.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to print select data from dataset

Sounds like you need to analyze (PROCs CONTENTS, PRINT, FREQ) your input file SASUSER.ALL_FILES and the SAS variables/columns it contains. Your PUT statement must either list SAS variables, literal text (quoted), and / or PUT statement control commands. And if you were told what to code, I'd suggest you refer back to that individual for further guidance or at least take some interest in reading some basics about SAS datasets, variables and DATA step programming.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,177

Re: How to print select data from dataset

this line controls what appears in the output report/text file.
> 33 put owner '/directory/smith.txt';

You should replace that with this[pre] put (_all_)(' ') ;[/pre] This puts all variables of sasuser.all_files into the output.
To print just your chosen list of data columns, replace (_all_)(' ') with that list .

good luck
Super Contributor
Super Contributor
Posts: 3,174

Re: How to print select data from dataset

Yes, a PUT statement - share what code you have that does not work.

Also, there's much to offer for code examples and supplemental conference topic papers available at the SAS support http://support.sas.com/ website, either using its SEARCH facility or you can use a Google advanced search argument and include the parameter site:sas.com to limit the search to just the SAS website.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

data step put statement write file site:sas.com
Occasional Contributor
Posts: 5

Re: How to print select data from dataset

data smith;
set all_files;
file '/directory/smith.txt' PRINT;
where username='smith''
put smith /directory/smith.txt; - (told to use - put username path filename)
run;

for information: smith=username
path= /directory/smith.txt
filename= smith.txt

When doing this it creates the filename smith.txt but writes smith /directory/smith.txt inside of the file.
Ask a Question
Discussion stats
  • 10 replies
  • 143 views
  • 0 likes
  • 4 in conversation