BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ghosh
Barite | Level 11

As a personal project I would like to import Instagram data which I have dummied up in the file igdata_dummy.txt

The data file structure is as follows, each on separate line:
1. Header record is as "Username's profile picture"
2. Username's ID 
3. Period "." if he Username is not followed back
4. Username's name (optional)

filename igdat  '~/dat/igdata_dummy.txt';
data want;
  infile igdat truncover end=eof;
  length header $200 ID_ig $50 Follower $1 Username $100 ;
      input @@ ;
      if scan(_infile_,-1)= 'picture' and not eof then do;
        input header &;
        input ID_ig;
       if _infile_= '.' then 
       input Follower ;       
        else input Username &;        
     end; 
     output;   
   run;
proc print n;
run;

Content of  file igdata_dummy.txt

Johnsmith's profile picture
Johnsmith
·
John Smith
BW_photo's profile picture
BW_photo
Ansel Adams
IG.user's profile picture
IG.user

Desired output

ghosh_0-1674417042106.png

I would appreciate your suggestions in importing this data set.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

OK. Try this one :

 

data have;
infile cards truncover ;
input have $100.;
cards;
Johnsmith's profile picture
Johnsmith
·
Johnsmith
BW_photo's profile picture
BW_photo
Ansel Adams
IG.user's profile picture
IG.user
IG.user
;
run;
data temp;
 set have;
 length temp name $ 100;
 retain temp found;
 if findw(have,'profile') and findw(have,'picture') then do;
  id+1;n=1;temp=scan(have,1,"'");name='header';found=0;
 end;
    else if  have=:temp and not found then do;n=2;name='ID_ig';found=1;end;
      else if strip(have)='·' or missing(have) then do;n=3;name='Follower';end;
	    else do;n=4;name='Username';end;
drop temp found;
run;
proc transpose data=temp out=want(drop=_:);
by id;
id name;
var have;
run;

Ksharp_0-1674533507716.png

 

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
infile cards truncover ;
input have $100.;
cards;
Johnsmith's profile picture
Johnsmith
·
John Smith
BW_photo's profile picture
BW_photo
Ansel Adams
IG.user's profile picture
IG.user
;
run;
data temp;
 set have;
 length temp name $ 100;
 retain temp;
 if findw(have,'profile') and findw(have,'picture') then do;
  id+1;n=1;temp=scan(have,1,"'");name='header';
 end;
    else if  have=:temp then do;n=2;name='ID_ig';end;
      else if strip(have)='·' or missing(have) then do;n=3;name='Follower';end;
	    else do;n=4;name='Username';end;
drop temp;
run;
proc transpose data=temp out=want(drop=_:);
by id;
id name;
var have;
run;


Ksharp_0-1674448889769.png

 

ghosh
Barite | Level 11

Hello @Ksharp 

Thanks for taking the time to give me a solution.  However, if the Username's ID and the Username's name have identical values it throws an error.  I have updated the dummy data to re-create the error shown below

data have;
infile cards truncover ;
input have $100.;
cards;
Johnsmith's profile picture
Johnsmith
·
Johnsmith
BW_photo's profile picture
BW_photo
Ansel Adams
IG.user's profile picture
IG.user
IG.user
;
run;
data temp;
 set have;
 length temp name $ 100;
 retain temp;
 if findw(have,'profile') and findw(have,'picture') then do;
  id+1;n=1;temp=scan(have,1,"'");name='header';
 end;
    else if  have=:temp then do;n=2;name='ID_ig';end;
      else if strip(have)='·' or missing(have) then do;n=3;name='Follower';end;
	    else do;n=4;name='Username';end;
drop temp;
run;
proc transpose data=temp out=want(drop=_:);
by id;
id name;
var have;
run;

ghosh_0-1674510090317.png

 

Ksharp
Super User

OK. Try this one :

 

data have;
infile cards truncover ;
input have $100.;
cards;
Johnsmith's profile picture
Johnsmith
·
Johnsmith
BW_photo's profile picture
BW_photo
Ansel Adams
IG.user's profile picture
IG.user
IG.user
;
run;
data temp;
 set have;
 length temp name $ 100;
 retain temp found;
 if findw(have,'profile') and findw(have,'picture') then do;
  id+1;n=1;temp=scan(have,1,"'");name='header';found=0;
 end;
    else if  have=:temp and not found then do;n=2;name='ID_ig';found=1;end;
      else if strip(have)='·' or missing(have) then do;n=3;name='Follower';end;
	    else do;n=4;name='Username';end;
drop temp found;
run;
proc transpose data=temp out=want(drop=_:);
by id;
id name;
var have;
run;

Ksharp_0-1674533507716.png

 

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
  • 4 replies
  • 590 views
  • 2 likes
  • 2 in conversation