DATA Step, Macro, Functions and more

find the next word

Reply
Contributor
Posts: 43

find the next word

I have a .lst file that contain information similar to what’s below.

I want to keep  the number next to the word LABEL.

For example:

Varname len1 len2 l      abel   ndif    maxdif

Aaaa        1        1      LABEL   4       .01

BBBB       2        2      LABEL   5       0.2

I want to keep only 

AAAA 4

BBBB  5

I should I do this in SAS?

Super Contributor
Posts: 307

Re: find the next word

Read the file into a dataset, then use a keep statement to keep the variables you want.

PROC Star
Posts: 7,361

Re: find the next word

filename test "c:\art\test.lst";

/*create .lst file*/

data _null_;

  file test;

  input;

  put _infile_;

  cards;

Varname len1 len2 l      abel   ndif    maxdif

Aaaa        1        1      LABEL   4       .01

BBBB       2        2      LABEL   5       0.2

;

data want;

  infile test firstobs=2;

  input varname $@;

  input @"LABEL" ndif;

run;

Respected Advisor
Posts: 3,124

Re: find the next word

That was super slick, Art. I know your solution is probably close to what OP want, but here is a trick from using Parmcards to make a stream-in data flow:

filename ft15f001 temp;

data _null_;

  input;

  parmcards;

Varname len1 len2 l abel ndif maxdif

Aaaa 1 1 LABEL 4 .01

BBBB 2 2 LABEL 5 0.2

;

data want;

  infile ft15f001 firstobs=2;

  input varname $@;

  input @"LABEL" ndif;

run;

Haikuo

Regular Contributor
Posts: 180

Re: find the next word

Haikuo:

When I run your code I get an error: No DATALINES or INFILE statement. But If I remove the input statement in the data _null_ step I get the same result without the error message.

Respected Advisor
Posts: 3,124

Re: find the next word

you are correct. It runs through even without data _null_;

filename ft15f001 temp;

  parmcards;

Varname len1 len2 l abel ndif maxdif

Aaaa 1 1 LABEL 4 .01

BBBB 2 2 LABEL 5 0.2

;

data want3;

  infile ft15f001 firstobs=2;

  input varname $@;

  input @"LABEL" ndif;

run;

Haikuo

Super User
Posts: 17,784

Re: find the next word

Possible thought: That looks like the output from proc compare, you can capture that using ODS statements and parse as well, instead of reading in the LST files.

PROC Star
Posts: 7,361

Re: find the next word

: The one thing you didn't criticize me for, but should have, is that my proposed code could be simplified to just:

data want;

  infile "c:\art\test.lst" firstobs=2;

  input varname $ @"LABEL" ndif;

run;

Contributor
Posts: 43

Re: find the next word

Reeza, you'r right it is result from proc compare. I have used ODS feature to capture the result  into a dataset. However, I’m having problem parsing the data because of SAs limitation to put each variable into its own variable.  SAS created two variable (batch and type) and if the ndif is longer than 6 digits it puts len2 with ndif. 

Super User
Posts: 17,784

Re: find the next word

If you only want the NDIF then use the outstats table filtered, and you can even filter out for only ones that are not equal to 0, though I haven't done that below.

SAS 9.3 :

data class;

    set sashelp.class;

    if age=14 then weight=0;

run;

proc compare data=sashelp.class compare=class outstats=check (where=(_type_="NDIF"));

run;

Ask a Question
Discussion stats
  • 9 replies
  • 401 views
  • 4 likes
  • 6 in conversation