DATA Step, Macro, Functions and more

Converting txt file into sas data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Converting txt file into sas data set

[ Edited ]

Dear All,

 

I need to execute a linux command and from de result create a data set:

 

%macro psawk;
%sysexec %str(ps aux | awk '{print $1,$2,$3,$6,$11}' >/SASData/test11.txt);
%mend psawk;
%psawk;

 

from this I get a file with the following structure

 

sas.png

 

Regards,

Abraham


Accepted Solutions
Solution
‎04-23-2018 03:58 PM
Trusted Advisor
Posts: 1,848

Re: Converting txt file into sas data set

Assign filename to the file created by the command and read it into sas:

 

filename result '/SASData/test11.txt';  

data want;
    infile result truncover firstobs=2;
    input user $ @; 
    if user = "USER" then do; input; return; end; /* skip 1st line */
    input  pid cpu rss command $;
run;
    

View solution in original post


All Replies
SAS Super FREQ
Posts: 9,433

Re: Converting txt file into sas data set

Hi:
In order to turn this file into a SAS data set, you will have to import it into SAS. You can do this a variety of ways -- either PROC IMPORT or a DATA step program. I would probably use a DATA step program.

What code did you try? What do you want the variables to be named. Note that SAS Variable names should start with a letter or underscore, so %CPU is not a SAS variable name.

cynthia

cynthia
Occasional Contributor
Posts: 6

Re: Converting txt file into sas data set

Posted in reply to Cynthia_sas

thanks for all your help

Solution
‎04-23-2018 03:58 PM
Trusted Advisor
Posts: 1,848

Re: Converting txt file into sas data set

Assign filename to the file created by the command and read it into sas:

 

filename result '/SASData/test11.txt';  

data want;
    infile result truncover firstobs=2;
    input user $ @; 
    if user = "USER" then do; input; return; end; /* skip 1st line */
    input  pid cpu rss command $;
run;
    
Occasional Contributor
Posts: 6

Re: Converting txt file into sas data set

Thanks for your answer, it it works but I have a problem in the command column just put a fragment and not the complete information, this field can have up to 400 characters.

 

Regards

SAS Super FREQ
Posts: 9,433

Re: Converting txt file into sas data set

Hi:
Then you need to have a Length statement:
LENGTH command $400;

Cynthia
Trusted Advisor
Posts: 1,848

Re: Converting txt file into sas data set

[ Edited ]

The default length is 8.

Change the $ after command to $400.  (include the dot).

Super User
Super User
Posts: 8,279

Re: Converting txt file into sas data set

Why create the text file at all?

data want;
    infile "ps aux | awk '{print $1,$2,$3,$6,$11}'" pipe firstobs=2 truncover ;
    input user :$40. pid cpu rss command $500.;
run;

 

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 186 views
  • 3 likes
  • 4 in conversation