Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • syntax from SAS to Stata

    I need to convert this code from SAS to Stata. I'm a newbie Stata user (2 months) (previously SPSS). I used SAS 30 years ago! I think its pretty straight forward!

    I'm using a government dataset and I want to create one record per person, and the new variables are whether or
    Data file has up to two records per person (17,259 records, 17,050 unique IDs (ABSHHID) 30 variables). Security issues wont let me convert long format to wide format. I actually am not even sure if the SAS code they sent me does what I want it to do.

    This is what I want to do on a made up dataset. Its fairly simple question I think, has each person ever owned a v1Dog v2Cat v3Rabbit?

    ABSHHID NowOrBefore OwnDog OwnCat OwnRabbit
    1 Now 1 1 0
    1 Before 1 0 1
    2 Now 0 0 0
    2 Before 1 1 0
    3 Now 0 0 0
    4 Now 0 0 0

    This is their code:

    DATA TEMP;
    SET PSS12EMA;

    PROC SORT;
    BY ABSHHID;

    DATA TEMP1 (KEEP = ABSHHID EATYPNA EATYPNB EATYPNC EATYPND EATCNT WTHECO);
    SET TEMP;
    BY ABSHHID;
    IF FIRST.ABSHHID THEN DO; EATYPNA=0; EATYPNB=0; EATYPNC=0; EATYPND=0; EATCNT=0; WTHECO=0; END;
    RETAIN EATYPNA EATYPNB EATYPNC EATYPND;
    IF EATYPCE=5 THEN EATYPNA=1;
    IF EATYPCF=6 THEN EATYPNB=1;
    IF EATYPCG=7 THEN EATYPNC=1;
    IF EATYPCH=8 THEN EATYPND=1;

    IF LAST.ABSHHID THEN DO;
    EATCNT=EATYPNA+EATYPNB+EATYPNC+EATYPND;
    IF 1<=EATCNT THEN WTHECO=1; ELSE WTHECO=5;
    OUTPUT;
    END;



    Tags: convert SAS, loop, syntax

  • #2
    You may get a response from somebody on this list who "speaks" SAS and can translate it for you. But there aren't all that many of them around here. Like you, I last used SAS about 30 years ago (and said more of a good riddance than a good bye to it). So I'm not able to directly help you.

    My first thought, if I were confronted with this situation, would be to find a colleague who uses SAS and ask them to run this for you in SAS. Then you could import the finished SAS data set into Stata using either Stat Transfer or the user written program -usesas- (available from SSC).

    That said, some things confuse me about your post. I don't understand what kind of "security issues" prevent converting long format to wide. The -reshape- command neither creates nor destroys information. Whatever security threats the data poses in long layout, it is the same in wide. Of course, whatever you ultimately want to do with the data will probably be easier in long layout anyway, so it's probably not a practical issue, but I'm really curious what you're referring to.

    Assuming that your goal is
    This is what I want to do on a made up dataset. Its fairly simple question I think, has each person ever owned a v1Dog v2Cat v3Rabbit?
    and ignoring the SAS code, I would solve this problem as follows:

    Code:
    reshape long Own, i(ABSHHID NowOrBefore) j(Animal) string
    egen ever_owned_any = max(Own), by(ABSHHID)
    If you want to avoid the -reshape- into double long layout, you can also do it this way (starting from the original data):

    Code:
    egen OwnAnAnimal = rowmax(Own*)
    egen ever_owned_any = max(OwnAnAnimal), by(ABSHHID)

    Comment


    • #3
      Dear Clyde,

      Thank you for your suggestion. That's a good idea asking a SAS person, but I think you found the solution for me in Stata. Your solution is neat. I will try that, then delete the duplicate id observations, then merge this file with another file (which has no duplicates).

      The security issue is an intriguing one indeed. The dataset belongs to the Australian government, and I can only access (with all the necessary permissions) it via their server. I cannot look or have a copy of the dataset, so I created my own Stata one on my local drive (with no data) (that way I can use the menus to generate code with the empty file with the relevant variables). So I can't even eyeball the data! I can list maybe up to 10 records with a few variables. Some commands are not even allowed - reshape being one of them! Its very frustrating - I should get my PhD just for putting up with this system. I send code, they send me back a text file with output! They use Stata v9, which is pretty good given that they accept code from SPSS v11, hence the main reason I have jumped the SPSS ship.

      I'll let you know how I get on.

      Thanks,

      Jozica


      Comment

      Working...
      X