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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1097 views
  • 0 likes
  • 4 in conversation