BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FollinLane
Obsidian | Level 7

Hi,

 

I'm reading in a file using the code below.  I'm new as using the scan function and would like to know how to output to the record after each iteration of through the code. 

 

Thank you

 

data testdata2;
    infile mysource lrecl = 6807 truncover scanover;
        input
            @'"UserEmail"|"metadata_value":"' user_email $100.
			@'"ProductVersion"|"metadata_value":"' prod_version $20.
			@'"SourceEvent"|"metadata_value":"' source_event $30.
			@'"ProcessTotalWaitTime"|"metadata_value":"' PT_WaitTime $10.
			;

        user_email = scan(user_email,1,'"');
		prod_version = scan(prod_version,1,'"');
		source_event = scan(source_event,1,'"');
		PT_WaitTime = scan(PT_WaitTime,1,'"');

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You may extract all fields and values with the help of regular expression processing:

 

data have;
if not prxId then prxId + prxParse('/"metadata_name":"(.*?)"\|"metadata_value":"(.*?)"/');
infile "&sasforum.\Datasets\test.txt" truncover;
input line $1200.;
obs + 1;
length field $32 value $128;
start = 1; stop = -1;
call prxnext(prxId, start, stop, line, pos, len);
do while (pos > 0);
    field = prxPosn(prxId, 1, line);
    value = prxPosn(prxId, 2, line);
    output;
    call prxnext(prxId, start, stop, line, pos, len);
    end;
drop prxId line start stop pos len; 
run;
     obs  field                             value
------------------------------------------------------------------------------------
       1  UserEmail                         user1@mycompany.org
       1  ProductVersion                    8.0.2009.0
       1  SourceEvent                       IdleEvent|1/30/2019 12:22:11 AM
       1  ProcessTotalWaitTime              0
       1  KeyPressCount                     2
       1  ErrorKeyPressCount                0
       1  CtrlC_Count                       0
       1  CtrlV_Count                       0
       1  CtrlX_Count                       0
       1  MouseClickCount                   4
       1  MouseWheelCount                   0
       1  WindowMoveSizeCount               0
       1  ScrollCount                       0
       1  HasScreenMapping                  True
       1  IsTestScreenMapping               False
       1  IsTestUser                        False
       2  UserEmail                         user2@mycompany.org
       2  ProductVersion                    8.0.2009.0
       2  SourceEvent                       ForegroundChanged|66570|0|0|0|179953
       2  ProcessTotalWaitTime              0
.....
PG

View solution in original post

5 REPLIES 5
Reeza
Super User

It's not clear what this means:

I'm new as using the scan function and would like to know how to output to the record after each iteration of through the code. 

Your new data set, testdata2, should have a record for each line in your source data file.

If you were trying to remove the quotes with the SCAN() function, COMPRESS() or DEQUOTE() which removes quotes.

 


@FollinLane wrote:

Hi,

 

I'm reading in a file using the code below.  I'm new as using the scan function and would like to know how to output to the record after each iteration of through the code. 

 

Thank you

 

data testdata2;
    infile mysource lrecl = 6807 truncover scanover;
        input
            @'"UserEmail"|"metadata_value":"' user_email $100.
			@'"ProductVersion"|"metadata_value":"' prod_version $20.
			@'"SourceEvent"|"metadata_value":"' source_event $30.
			@'"ProcessTotalWaitTime"|"metadata_value":"' PT_WaitTime $10.
			;

        user_email = scan(user_email,1,'"');
		prod_version = scan(prod_version,1,'"');
		source_event = scan(source_event,1,'"');
		PT_WaitTime = scan(PT_WaitTime,1,'"');

run;

 


 

PGStats
Opal | Level 21

You may extract all fields and values with the help of regular expression processing:

 

data have;
if not prxId then prxId + prxParse('/"metadata_name":"(.*?)"\|"metadata_value":"(.*?)"/');
infile "&sasforum.\Datasets\test.txt" truncover;
input line $1200.;
obs + 1;
length field $32 value $128;
start = 1; stop = -1;
call prxnext(prxId, start, stop, line, pos, len);
do while (pos > 0);
    field = prxPosn(prxId, 1, line);
    value = prxPosn(prxId, 2, line);
    output;
    call prxnext(prxId, start, stop, line, pos, len);
    end;
drop prxId line start stop pos len; 
run;
     obs  field                             value
------------------------------------------------------------------------------------
       1  UserEmail                         user1@mycompany.org
       1  ProductVersion                    8.0.2009.0
       1  SourceEvent                       IdleEvent|1/30/2019 12:22:11 AM
       1  ProcessTotalWaitTime              0
       1  KeyPressCount                     2
       1  ErrorKeyPressCount                0
       1  CtrlC_Count                       0
       1  CtrlV_Count                       0
       1  CtrlX_Count                       0
       1  MouseClickCount                   4
       1  MouseWheelCount                   0
       1  WindowMoveSizeCount               0
       1  ScrollCount                       0
       1  HasScreenMapping                  True
       1  IsTestScreenMapping               False
       1  IsTestUser                        False
       2  UserEmail                         user2@mycompany.org
       2  ProductVersion                    8.0.2009.0
       2  SourceEvent                       ForegroundChanged|66570|0|0|0|179953
       2  ProcessTotalWaitTime              0
.....
PG
ErikLund_Jensen
Rhodochrosite | Level 12

@PGStats 

This is really good. Thanks!

 

@FollinLane 

Should "output after each iteration" be interpreted as output after each variable is extracted or output after processing of each record. In the latter case, you could add a transpose step after PGStats' excellent code:

 


proc transpose data=have out=want let;
   by obs;
   id field;
   var value;
 run;

 

FollinLane
Obsidian | Level 7
Sorry for the omission but I was trying to output after processing each record. For the sake of getting too detailed I got some strange results where the output seemed to be missing records or showing a partial record. Thanks for the the additional help!
FollinLane
Obsidian | Level 7

This helps a lot.  I will need to study it more.  Along with what ErikLund_Jensen posted I think I have the solution.

 

Thank you very much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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