BookmarkSubscribeRSS Feed
tes218
Calcite | Level 5
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.
10 REPLIES 10
Bill
Quartz | Level 8
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
tes218
Calcite | Level 5
I have the dataset but need to create a text file on a Linux server with just that one user's information in it.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
tes218
Calcite | Level 5
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Can you explain what the problem is?

Scott Barry
SBBWorks, Inc.
tes218
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Peter_C
Rhodochrosite | Level 12
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
tes218
Calcite | Level 5
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 956 views
  • 0 likes
  • 4 in conversation